<?php

/**
 * ============================================================
 *
 * @project   biiproject
 *
 * @author    Andika Debi Putra
 *
 * @email     andikadebiputra@gmail.com
 *
 * @website   https://biiproject.com
 *
 * @copyright Copyright (c) 2026 Andika Debi Putra
 * @license   Proprietary - All Rights Reserved
 *
 * @version   1.0.0
 *
 * @created   2026-05-01
 * ============================================================
 */

namespace App\Services\AI;

class AiAssistantService
{
    public function __construct(
        protected AiService $aiService
    ) {}

    /**
     * Answer questions about the system.
     */
    public function answer(string $question): string
    {
        if (! get_setting('ai_enabled', false)) {
            return 'AI Assistant is currently disabled.';
        }

        $systemPrompt = "You are the BIIProject Intelligent Assistant. 
        You help administrators manage the system. 
        The system is built with Laravel, PostgreSQL, and Redis.
        Key features: RBAC, System Monitoring, AI Log Analysis, Backup/Restore.
        
        Guidelines:
        - Be professional, helpful, and concise.
        - If asked about technical details, provide accurate Laravel-based advice.
        - If you don't know the specific configuration of this instance, advise checking 'System Config'.
        - Security is priority. Never advise actions that compromise security.";

        try {
            $result = $this->aiService->provider()->generate($question, [
                'system_instruction' => $systemPrompt,
            ]);

            if (isset($result['success']) && $result['success']) {
                return $result['response'];
            }

            return 'Sorry, I encountered an error: '.($result['error'] ?? 'Unknown error');
        } catch (\Exception $e) {
            return 'Error: '.$e->getMessage();
        }
    }
}
