<?php

namespace OtomaticAi\Controllers;

use OtomaticAi\Models\Usages\AnthropicUsage;
use OtomaticAi\Models\Usages\DeepSeekChatUsage;
use OtomaticAi\Models\Usages\FluxUsage;
use OtomaticAi\Models\Usages\GeminiChatUsage;
use OtomaticAi\Models\Usages\GeminiImageUsage;
use OtomaticAi\Models\Usages\IdeogramUsage;
use OtomaticAi\Models\Usages\MistralAIUsage;
use OtomaticAi\Models\Usages\OpenAIChatUsage;
use OtomaticAi\Models\Usages\OpenAIImageUsage;
use OtomaticAi\Models\Usages\StabilityAIUsage;
use OtomaticAi\Models\Usages\XAIChatUsage;
use OtomaticAi\Models\Usages\XAIImageUsage;
use OtomaticAi\Vendors\Illuminate\Support\Arr;

class UsageController extends Controller
{
    public function __invoke()
    {
        $this->verifyNonce();

        $this->validate([
            "provider" => ["nullable"],
        ]);

        switch ($this->input('provider')) {
            case "openai":
                $usages = [];

                $chatUsages = OpenAIChatUsage::monthly(12);
                $imageUsages = OpenAIImageUsage::monthly(12);

                $keys = array_unique(array_merge(array_keys($chatUsages), array_keys($imageUsages)));
                sort($keys);

                foreach ($keys as $key) {
                    $usages[$key] = [
                        "amount" => Arr::get($chatUsages, $key . '.amount', 0) + Arr::get($imageUsages, $key . '.amount', 0),
                        "details" => array_merge(Arr::get($chatUsages, $key . '.details', []), Arr::get($imageUsages, $key . '.details', []))
                    ];
                }

                return $this->response($usages);
                break;
            case "x_ai":
                $usages = [];

                $chatUsages = XAIChatUsage::monthly(12);
                $imageUsages = XAIImageUsage::monthly(12);

                $keys = array_unique(array_merge(array_keys($chatUsages), array_keys($imageUsages)));
                sort($keys);

                foreach ($keys as $key) {
                    $usages[$key] = [
                        "amount" => Arr::get($chatUsages, $key . '.amount', 0) + Arr::get($imageUsages, $key . '.amount', 0),
                        "details" => array_merge(Arr::get($chatUsages, $key . '.details', []), Arr::get($imageUsages, $key . '.details', []))
                    ];
                }

                return $this->response($usages);
                break;
            case "mistral_ai":
                $usages = MistralAIUsage::monthly(12);
                return $this->response($usages);
                break;
            case "anthropic":
                $usages = AnthropicUsage::monthly(12);
                return $this->response($usages);
                break;
            case "stability_ai":
                $usages = StabilityAIUsage::monthly(12);
                return $this->response($usages);
                break;
            case "flux":
                $usages = FluxUsage::monthly(12);
                return $this->response($usages);
                break;
            case "ideogram":
                $usages = IdeogramUsage::monthly(12);
                return $this->response($usages);
                break;
            case "deepseek":
                $usages = DeepSeekChatUsage::monthly(12);
                return $this->response($usages);
                break;
            case "gemini":
                $usages = [];

                $chatUsages = GeminiChatUsage::monthly(12);
                $imageUsages = GeminiImageUsage::monthly(12);

                $keys = array_unique(array_merge(array_keys($chatUsages), array_keys($imageUsages)));
                sort($keys);

                foreach ($keys as $key) {
                    $usages[$key] = [
                        "amount" => Arr::get($chatUsages, $key . '.amount', 0) + Arr::get($imageUsages, $key . '.amount', 0),
                        "details" => array_merge(Arr::get($chatUsages, $key . '.details', []), Arr::get($imageUsages, $key . '.details', []))
                    ];
                }

                return $this->response($usages);
                break;
            default:
                $usages = [];

                // text
                $openAIChatUsages = OpenAIChatUsage::monthly(12);
                $mistralAIUsages = MistralAIUsage::monthly(12);
                $anthropicUsages = AnthropicUsage::monthly(12);
                $deepSeekChatUsages = DeepSeekChatUsage::monthly(12);
                $geminiChatUsages = GeminiChatUsage::monthly(12);
                $xAiChatUsages = XAIChatUsage::monthly(12);

                // image
                $openAIImageUsages = OpenAIImageUsage::monthly(12);
                $stabilityAIUsages = StabilityAIUsage::monthly(12);
                $fluxUsages = FluxUsage::monthly(12);
                $ideogramUsages = IdeogramUsage::monthly(12);
                $geminiImageUsages = GeminiImageUsage::monthly(12);
                $xAiImageUsages = XAIImageUsage::monthly(12);

                $keys = array_unique(
                    array_merge(
                        array_keys($openAIChatUsages),
                        array_keys($openAIImageUsages),
                        array_keys($mistralAIUsages),
                        array_keys($stabilityAIUsages),
                        array_keys($anthropicUsages),
                        array_keys($fluxUsages),
                        array_keys($ideogramUsages),
                        array_keys($deepSeekChatUsages),
                        array_keys($geminiChatUsages),
                        array_keys($geminiImageUsages),
                        array_keys($xAiChatUsages),
                        array_keys($xAiImageUsages)
                    )
                );
                sort($keys);

                foreach ($keys as $key) {
                    $amount = 0;
                    $amount += Arr::get($openAIChatUsages, $key . '.amount', 0);
                    $amount += Arr::get($openAIImageUsages, $key . '.amount', 0);
                    $amount += Arr::get($stabilityAIUsages, $key . '.amount', 0);
                    $amount += Arr::get($mistralAIUsages, $key . '.amount', 0);
                    $amount += Arr::get($anthropicUsages, $key . '.amount', 0);
                    $amount += Arr::get($fluxUsages, $key . '.amount', 0);
                    $amount += Arr::get($ideogramUsages, $key . '.amount', 0);
                    $amount += Arr::get($deepSeekChatUsages, $key . '.amount', 0);
                    $amount += Arr::get($geminiChatUsages, $key . '.amount', 0);
                    $amount += Arr::get($geminiImageUsages, $key . '.amount', 0);
                    $amount += Arr::get($xAiChatUsages, $key . '.amount', 0);
                    $amount += Arr::get($xAiImageUsages, $key . '.amount', 0);

                    $details = [];
                    $details = array_merge($details, Arr::get($openAIChatUsages, $key . '.details', []));
                    $details = array_merge($details, Arr::get($openAIImageUsages, $key . '.details', []));
                    $details = array_merge($details, Arr::get($stabilityAIUsages, $key . '.details', []));
                    $details = array_merge($details, Arr::get($mistralAIUsages, $key . '.details', []));
                    $details = array_merge($details, Arr::get($anthropicUsages, $key . '.details', []));
                    $details = array_merge($details, Arr::get($fluxUsages, $key . '.details', []));
                    $details = array_merge($details, Arr::get($ideogramUsages, $key . '.details', []));
                    $details = array_merge($details, Arr::get($deepSeekChatUsages, $key . '.details', []));
                    $details = array_merge($details, Arr::get($geminiChatUsages, $key . '.details', []));
                    $details = array_merge($details, Arr::get($geminiImageUsages, $key . '.details', []));
                    $details = array_merge($details, Arr::get($xAiChatUsages, $key . '.details', []));
                    $details = array_merge($details, Arr::get($xAiImageUsages, $key . '.details', []));

                    $usages[$key] = [
                        "amount" => $amount,
                        "details" => $details
                    ];
                }

                return $this->response($usages);
        }

        $this->response();
    }
}
