<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Services\Ai\AiSettingsManager;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class AiSettingsController extends Controller
{
    public function index(AiSettingsManager $settingsManager): View
    {
        $page_title = 'AI Yorum Ayarları';
        $page_description = 'Raporlar için yönetici özeti, risk, fırsat ve aksiyon önerisi üretimi';
        $settings = $settingsManager->all();
        $maskedApiKey = $settingsManager->maskedApiKey();

        return view('admin.ai.settings', compact('page_title', 'page_description', 'settings', 'maskedApiKey'));
    }

    public function save(Request $request, AiSettingsManager $settingsManager): RedirectResponse
    {
        $data = $request->validate([
            'enabled' => ['nullable', 'boolean'],
            'provider' => ['required', 'in:local,openai_compatible'],
            'endpoint' => ['nullable', 'url', 'max:500'],
            'model' => ['nullable', 'string', 'max:120'],
            'temperature' => ['nullable', 'numeric', 'min:0', 'max:1'],
            'timeout' => ['nullable', 'integer', 'min:10', 'max:120'],
            'api_key' => ['nullable', 'string', 'max:2000'],
        ]);

        $settingsManager->save($data);

        return back()->with('success', 'AI yorum ayarları kaydedildi.');
    }
}
