#!/usr/bin/env raku
use v6.d;

use WWW::OpenRouter;

my %*SUB-MAIN-OPTS =
        :named-anywhere,
        # allow named variables at any location
        ;

#| Text processing using the OpenRouter API.
multi sub openrouter-front(Str $text,                               #= Text to be processed or audio file name.
                          Str :$path = 'chat/completions',          #= Path, one of 'chat/completions', 'embeddings', or 'models'.
                          UInt :mt(:$max-tokens) = 2048,            #= The maximum number of tokens to generate in the completion.
                          Str :m(:$model) is copy = 'Whatever',     #= Model.
                          Str :r(:$role) is copy = 'user',          #= Role.
                          Real :t(:$temperature) = 0.7,             #= Temperature.
                          Str :$response-format = 'url',            #= The format in which the response is returned.
                          Str :a(:$auth-key) is copy = 'Whatever',  #= Authorization key (to use OpenRouter API.)
                          UInt :$timeout= 10,                       #= Timeout.
                          Str :f(:$format) is copy = 'Whatever',    #= Format of the result; one of "json", "hash", "values", or "Whatever".
                          Str :$method is copy = 'tiny',            #= Method for the HTTP POST query; one of "tiny" or "curl".
                          ) {

    if $text.chars == 0 {
        note 'Nothing.';
        return;
    }

    if $auth-key eq 'Whatever' {
        if %*ENV<OPENROUTER_API_KEY>:exists {
            $auth-key = %*ENV<OPENROUTER_API_KEY>;
        } else {
            note 'Cannot find OpenRouter authorization key. ' ~
                    'Please provide a valid key to the argument auth-key, or set the ENV variable OPENROUTER_API_KEY.';
            $auth-key = ''
        }
    }

    if $format.lc ∈ <v value auto whatever> { $format = 'values'; }

    my $res =
            openrouter-playground($text,
                    :$path,
                    model => $model eq 'Whatever' ?? Whatever !! $model,
                    role => $role eq 'Whatever' ?? Whatever !! $role,
                    :$max-tokens,
                    :$response-format,
                    :$temperature,
                    :$auth-key,
                    :$timeout,
                    :$format,
                    :$method);

    if $format.lc ∈ <hash raku> {
        say $res.raku;
    } else {
        say $res;
    }
}

multi sub MAIN
#= Command given as a sequence of words.
(*@words,
 Str :$path = 'chat/completions',          #= Path, one of 'chat/completions', 'images/generations', 'images/edits', 'images/variations', 'moderations', 'audio/transcriptions', 'audio/translations', 'embeddings', or 'models'.
 UInt :mt(:$max-tokens) = 2048,            #= The maximum number of tokens to generate in the completion.
 Str :m(:$model) is copy = 'Whatever',     #= Model.
 Str :r(:$role) is copy = 'user',          #= Role.
 Real :t(:$temperature) = 0.7,             #= Temperature.
 Str :$response-format = 'url',            #= The format in which the response is returned.
 Str :a(:$auth-key) is copy = 'Whatever',  #= Authorization key (to use OpenRouter API.)
 UInt :$timeout= 10,                       #= Timeout.
 Str :f(:$format) is copy = 'Whatever',    #= Format of the result; one of "json", "hash", "values", or "Whatever".
 Str :$method is copy = 'tiny',            #= Method for the HTTP POST query; one of "tiny" or "curl".
 ) {
    return openrouter-front(@words.join(' ').Str, :$model, :$path, :$max-tokens, :$role, :$temperature, :$response-format, :$auth-key, :$timeout, :$format, :$method);
}