<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class AiSetting extends Model
{
    use HasFactory;

    /**
     * Provider types
     */
    public const PROVIDER_OPENAI = 'openai';
    public const PROVIDER_OLLAMA = 'ollama';
    public const PROVIDER_LMSTUDIO = 'lmstudio';
    public const PROVIDER_CUSTOM = 'custom';

    /**
     * Default API URLs for providers
     */
    public const DEFAULT_URLS = [
        self::PROVIDER_OPENAI => 'https://api.openai.com/v1',
        self::PROVIDER_OLLAMA => 'http://localhost:11434/v1',
        self::PROVIDER_LMSTUDIO => 'http://localhost:1234/v1',
        self::PROVIDER_CUSTOM => '',
    ];

    /**
     * Default models for providers
     */
    public const DEFAULT_MODELS = [
        self::PROVIDER_OPENAI => 'gpt-3.5-turbo',
        self::PROVIDER_OLLAMA => 'llama2',
        self::PROVIDER_LMSTUDIO => 'local-model',
        self::PROVIDER_CUSTOM => 'custom-model',
    ];

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'provider',
        'name',
        'api_key',
        'api_url',
        'model',
        'system_prompt',
        'is_active',
        'max_tokens',
        'temperature',
        'max_history',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'is_active' => 'boolean',
        'max_tokens' => 'integer',
        'temperature' => 'float',
        'max_history' => 'integer',
    ];

    /**
     * Get the active AI setting.
     *
     * @return self|null
     */
    public static function getActive(): ?self
    {
        return static::where('is_active', true)->first();
    }

    /**
     * Get all settings for a specific provider.
     *
     * @param string $provider
     * @return \Illuminate\Database\Eloquent\Collection
     */
    public static function getForProvider(string $provider): \Illuminate\Database\Eloquent\Collection
    {
        return static::where('provider', $provider)->get();
    }

    /**
     * Scope to query only active settings.
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    /**
     * Set this setting as active and deactivate all others.
     *
     * @return bool
     */
    public function setActive(): bool
    {
        // Deactivate all other settings
        static::where('id', '!=', $this->id)->update(['is_active' => false]);

        // Activate this setting
        $this->is_active = true;
        return $this->save();
    }

    /**
     * Get provider label.
     *
     * @return string
     */
    public function getProviderLabelAttribute(): string
    {
        return match ($this->provider) {
            self::PROVIDER_OPENAI => 'OpenAI',
            self::PROVIDER_OLLAMA => 'Ollama',
            self::PROVIDER_LMSTUDIO => 'LM Studio',
            self::PROVIDER_CUSTOM => 'Custom',
            default => 'Bilinmeyen',
        };
    }

    /**
     * Get provider icon.
     *
     * @return string
     */
    public function getProviderIconAttribute(): string
    {
        return match ($this->provider) {
            self::PROVIDER_OPENAI => '🤖',
            self::PROVIDER_OLLAMA => '🦙',
            self::PROVIDER_LMSTUDIO => '💻',
            self::PROVIDER_CUSTOM => '⚙️',
            default => '❓',
        };
    }

    /**
     * Check if this provider requires an API key.
     *
     * @return bool
     */
    public function requiresApiKey(): bool
    {
        return in_array($this->provider, [
            self::PROVIDER_OPENAI,
            self::PROVIDER_CUSTOM,
        ]);
    }

    /**
     * Get available providers.
     *
     * @return array<string, string>
     */
    public static function getAvailableProviders(): array
    {
        return [
            self::PROVIDER_OPENAI => 'OpenAI (Ücretli)',
            self::PROVIDER_OLLAMA => 'Ollama (Ücretsiz - Yerel)',
            self::PROVIDER_LMSTUDIO => 'LM Studio (Ücretsiz - Yerel)',
            self::PROVIDER_CUSTOM => 'Custom API',
        ];
    }
}
