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

use WWW::Ollama;

my %*SUB-MAIN-OPTS = :named-anywhere;

#| Ollama client invocation.
sub MAIN(
        *@words,
        Str :$path = 'completion',              #= Path, one of 'completion', 'chat', 'embedding', 'model-info', 'list-models', or 'list-running-models'.
        Str :m(:$model) is copy = 'Whatever',   #= Model to use.
        Str :f(:$format) is copy = 'Whatever',  #= Format of the result; one of "json", "hash", "values", or "Whatever".
         ) {

    if $model.lc ∈ <whatever auto automatic> { $model = 'gemma3:1b' }
    if $format.lc ∈ <v value auto whatever> { $format = 'values' }

    # TODO Check for valid JSON message input and/or JSON file if $path ∈ <chat chat-completion>

    my $res =
            ollama-client(
            @words.join(' '),
            :$path,
            model => $model.lc eq 'whatever' ?? Whatever !! $model,
            format => $format.lc eq 'whatever' ?? Whatever !! $format,
            client => Whatever
            );

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