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

use WWW::XAI;

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

#| API access to XAI LLMs.
multi sub MAIN(Str $text,                                #= Text to be processed or audio file name.
               Str :$path is copy = 'Whatever',          #= Path, one of "chat", "code", "image", "video", "voice", or "Whatever".
               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 :$video-id = 'Whatever',              #= Video identifier to retrieve record of.
               Str :a(:$auth-key) is copy = 'Whatever',  #= Authorization key (to use XAI 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 && $path.lc ne 'models' && $video-id.lc eq 'whatever' {
        note 'Nothing.';
        return;
    }

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

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

    if $path.lc ∈ <whatever auto automatic> {
        $path = $video-id.lc eq 'whatever' ?? 'chat' !! 'videos'
    }

    my $res =
           xai-console($text,
                    :$path,
                    model => $model.lc eq 'whatever' ?? Whatever !! $model,
                    role => $role.lc eq 'whatever' ?? Whatever !! $role,
                    :$max-tokens,
                    :$temperature,
                    :$auth-key,
                    :$timeout,
                    format => $format.lc eq 'whatever' ?? Whatever !! $format,
                    :$method,
                    :$response-format,
                    video-id => $video-id.lc eq 'whatever' ?? Whatever !! $video-id,
                    :!echo
           );

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

multi sub MAIN(
        *@words,
        Str :$path = 'chat',                      #= Path, one of 'chat', 'code', 'image', 'video', or 'voice'.
        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 :$video-id = 'Whatever',              #= Video identifier to retrieve record of.
        Str :a(:$auth-key) is copy = 'Whatever',  #= Authorization key (to use XAI 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".
               ) {
    MAIN(@words.join(' ').Str, :$path, :$max-tokens, :$model, :$role, :$temperature, :$response-format, :$video-id, :$auth-key, :$timeout, :$format, :$method)
}