<?php

namespace App\Services;

use App\Core\Database;

class HomepageCopilotService
{
    public static function interpret(
        string $prompt
    ): array
    {
        $db=
        Database::getInstance();

        $stmt=
        $db
        ->query("

SELECT
action_key,
intent_examples,
endpoint

FROM
conference_home_ai_actions

WHERE is_enabled=1

");

        $actions=
        $stmt
        ->fetchAll();

        foreach(
            $actions
            as $a
        ){

            foreach(

                explode(
                    ';',
                    strtolower(
                        $a['intent_examples']
                    )
                )

                as $k

            ){

                if(
                    str_contains(
                        strtolower($prompt),
                        trim($k)
                    )
                ){

                    return [

                        'matched'=>
                        true,

                        'action'=>
                        $a['action_key'],

                        'endpoint'=>
                        $a['endpoint'],

                        'explanation'=>

                        'Recommended action only'

                    ];

                }

            }

        }

        return [

            'matched'=>false,

            'action'=>null,

            'endpoint'=>null,

            'explanation'=>

            'No matching operation'

        ];

    }
}
