# vim: set filetype=python fileencoding=utf-8:
# -*- coding: utf-8 -*-

#============================================================================#
#                                                                            #
#  Licensed under the Apache License, Version 2.0 (the "License");           #
#  you may not use this file except in compliance with the License.          #
#  You may obtain a copy of the License at                                   #
#                                                                            #
#      http://www.apache.org/licenses/LICENSE-2.0                            #
#                                                                            #
#  Unless required by applicable law or agreed to in writing, software       #
#  distributed under the License is distributed on an "AS IS" BASIS,         #
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  #
#  See the License for the specific language governing permissions and       #
#  limitations under the License.                                            #
#                                                                            #
#============================================================================#


''' Mimeogram prompt text for LLMs. '''
# TODO? Use BSD sysexits.


from . import __
from . import exceptions as _exceptions
from . import interfaces as _interfaces


_scribe = __.produce_scribe( __name__ )


class Command(
    _interfaces.CliCommand,
    decorators = ( __.standard_tyro_class, ),
):
    ''' Provides LLM prompt text for mimeogram format. '''

    clip: __.typx.Annotated[
        __.tyro.conf.DisallowNone[ bool | None ],
        __.typx.Doc( ''' Copy prompt to clipboard. ''' ),
        __.tyro.conf.arg( aliases = ( '--clipboard', '--to-clipboard' ) ),
    ] = None

    async def __call__(
        self, auxdata: __.appcore.state.Globals
    ) -> None:
        ''' Executes command to provide prompt text. '''
        await provide_prompt( auxdata )

    def provide_configuration_edits(
        self,
    ) -> __.appcore.dictedits.Edits:
        ''' Provides edits against configuration from options. '''
        edits: list[ __.appcore.dictedits.Edit ] = [ ]
        if None is not self.clip:
            edits.append( __.appcore.dictedits.SimpleEdit( # pyright: ignore
                address = ( 'prompt', 'to-clipboard' ), value = self.clip ) )
        return tuple( edits )


async def acquire_prompt(
    auxdata: __.appcore.state.Globals,
) -> str:
    ''' Acquires prompt text from package data. '''
    location = (
        auxdata.distribution.provide_data_location(
            'prompts', 'mimeogram.md' ) )
    return await __.appcore.io.acquire_text_file_async( location )


async def provide_prompt(
    auxdata: __.appcore.state.Globals,
) -> None:
    ''' Provides mimeogram prompt text. '''
    with _exceptions.report_exceptions(
        _scribe, "Could not acquire prompt text."
    ):
        prompt = await acquire_prompt( auxdata )
    options = auxdata.configuration.get( 'prompt', { } )
    if options.get( 'to-clipboard', False ):
        from . import clipboard
        with _exceptions.report_exceptions(
            _scribe, "Could not copy prompt to clipboard."
        ): clipboard.copy_to_clipboard( prompt )
        _scribe.info( "Copied prompt to clipboard." )
    else: print( prompt ) # TODO? Use output stream from configuration.
    raise SystemExit( 0 )
