<?php

namespace ZenoChatbot\Services\LLMService\AIClient\Config;

if (!defined('ABSPATH')) {
    exit;
}

class AiConfig
{
    private string $providerClass;
    private string $model;
    private string $apiKey;
    private float $temperature;
    private int $maxTokens;

    public function __construct(array $data)
    {
        $this->providerClass = (string)($data['provider'] ?? '');
        $this->model = (string)($data['model'] ?? '');
        $this->apiKey = (string)($data['api_key'] ?? '');
        $this->temperature = (float)($data['temperature'] ?? 0.7);
        $this->maxTokens = (int)($data['max_tokens'] ?? 1000);
    }

    public static function provider(string $providerClass): ConfigBuilder
    {
        return ConfigBuilder::create()->provider($providerClass);
    }

    public static function fromArray(array $data): ConfigBuilder
    {
        return ConfigBuilder::create()->fromArray($data);
    }

    /**
     * @param AiConfig|array $config
     * @return AiConfig|ConfigBuilder
     */
    public static function create($config)
    {
        if ($config instanceof static) {
            return $config;
        }
        if (is_array($config)) {
            return static::fromArray($config);
        }
        throw new \InvalidArgumentException('AiConfig::create expects AiConfig or array.');
    }


    public function getProviderClass(): string
    {
        return $this->providerClass;
    }

    public function getModel(): string
    {
        return $this->model;
    }

    public function getApiKey(): string
    {
        return $this->apiKey;
    }

    public function getTemperature(): float
    {
        return $this->temperature;
    }

    public function getMaxTokens(): int
    {
        return $this->maxTokens;
    }

    public function toArray(): array
    {
        return [
            'provider'    => $this->providerClass,
            'model'       => $this->model,
            'api_key'     => $this->apiKey,
            'temperature' => $this->temperature,
            'max_tokens'  => $this->maxTokens,
        ];
    }
}

