<?php

namespace App\Helpers;

use App\Models\System\AiPricingConfig;
use App\Models\System\AiTokenUsage;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Str;

class AiTokenHelper
{
    /**
     * Gemini API 응답에서 토큰 사용량 저장
     */
    public static function saveGeminiUsage(array $apiResult, string $model, string $menuName): void
    {
        try {
            $usage = $apiResult['usageMetadata'] ?? null;
            if (! $usage) {
                return;
            }

            $promptTokens = $usage['promptTokenCount'] ?? 0;
            $completionTokens = $usage['candidatesTokenCount'] ?? 0;
            $totalTokens = $usage['totalTokenCount'] ?? 0;

            // DB 단가 조회 (fallback: 하드코딩 기본값)
            $pricing = AiPricingConfig::getActivePricing('gemini');
            $inputPrice = $pricing ? (float) $pricing->input_price_per_million / 1_000_000 : 0.10 / 1_000_000;
            $outputPrice = $pricing ? (float) $pricing->output_price_per_million / 1_000_000 : 0.40 / 1_000_000;

            self::save($model, $menuName, $promptTokens, $completionTokens, $totalTokens, $inputPrice, $outputPrice);
        } catch (\Exception $e) {
            Log::warning('AI token usage save failed (Gemini)', ['error' => $e->getMessage()]);
        }
    }

    /**
     * Claude API 응답에서 토큰 사용량 저장
     */
    public static function saveClaudeUsage(array $apiResult, string $model, string $menuName): void
    {
        try {
            $usage = $apiResult['usage'] ?? null;
            if (! $usage) {
                return;
            }

            $promptTokens = $usage['input_tokens'] ?? 0;
            $completionTokens = $usage['output_tokens'] ?? 0;
            $totalTokens = $promptTokens + $completionTokens;

            // DB 단가 조회 (fallback: 하드코딩 기본값)
            $pricing = AiPricingConfig::getActivePricing('claude');
            $inputPrice = $pricing ? (float) $pricing->input_price_per_million / 1_000_000 : 0.25 / 1_000_000;
            $outputPrice = $pricing ? (float) $pricing->output_price_per_million / 1_000_000 : 1.25 / 1_000_000;

            self::save($model, $menuName, $promptTokens, $completionTokens, $totalTokens, $inputPrice, $outputPrice);
        } catch (\Exception $e) {
            Log::warning('AI token usage save failed (Claude)', ['error' => $e->getMessage()]);
        }
    }

    /**
     * Google Cloud Storage 업로드 사용량 저장
     * GCS Class A 오퍼레이션: $0.005 / 1,000건, Storage: $0.02 / GB / month
     */
    public static function saveGcsStorageUsage(string $menuName, int $fileSizeBytes): void
    {
        try {
            $fileSizeMB = $fileSizeBytes / (1024 * 1024);
            // DB 단가 조회 (fallback: 하드코딩 기본값)
            $pricing = AiPricingConfig::getActivePricing('google-gcs');
            $unitPrice = $pricing ? (float) $pricing->unit_price : 0.005;
            $operationCost = $unitPrice / 1000;
            $storageCost = ($fileSizeMB / 1024) * 0.02 * (7 / 30); // 7일 보관 기준
            $costUsd = $operationCost + $storageCost;

            self::save('google-cloud-storage', $menuName, $fileSizeBytes, 0, $fileSizeBytes, $costUsd / max($fileSizeBytes, 1), 0);
        } catch (\Exception $e) {
            Log::warning('AI token usage save failed (GCS)', ['error' => $e->getMessage()]);
        }
    }

    /**
     * Google Speech-to-Text 사용량 저장
     * STT latest_long 모델: $0.009 / 15초
     */
    public static function saveSttUsage(string $menuName, int $durationSeconds): void
    {
        try {
            // DB 단가 조회 (fallback: 하드코딩 기본값)
            $pricing = AiPricingConfig::getActivePricing('google-stt');
            $sttUnitPrice = $pricing ? (float) $pricing->unit_price : 0.009;
            $costUsd = ceil($durationSeconds / 15) * $sttUnitPrice;

            self::save('google-speech-to-text', $menuName, $durationSeconds, 0, $durationSeconds, $costUsd / max($durationSeconds, 1), 0);
        } catch (\Exception $e) {
            Log::warning('AI token usage save failed (STT)', ['error' => $e->getMessage()]);
        }
    }

    /**
     * 공통 저장 로직
     */
    private static function save(
        string $model,
        string $menuName,
        int $promptTokens,
        int $completionTokens,
        int $totalTokens,
        float $inputPricePerToken,
        float $outputPricePerToken,
    ): void {
        $costUsd = ($promptTokens * $inputPricePerToken) + ($completionTokens * $outputPricePerToken);
        $exchangeRate = AiPricingConfig::getExchangeRate();
        $costKrw = $costUsd * $exchangeRate;

        $tenantId = session('selected_tenant_id', 1);

        AiTokenUsage::create([
            'tenant_id' => $tenantId,
            'model' => $model,
            'menu_name' => $menuName,
            'prompt_tokens' => $promptTokens,
            'completion_tokens' => $completionTokens,
            'total_tokens' => $totalTokens,
            'cost_usd' => $costUsd,
            'cost_krw' => $costKrw,
            'request_id' => Str::uuid()->toString(),
            'created_by' => auth()->id(),
        ]);
    }
}
