<?php

namespace App\Support\Pricing;

use App\Models\CatalogPriceRule;
use App\Models\SerenataPackage;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;

class CatalogPriceResolver
{
    public static function resolve(Model $priceable, ?string $date = null): array
    {
        $forDate = $date ? Carbon::parse($date) : Carbon::today();

        $baseCustomer = (float) ($priceable->base_price ?? 0);
        $baseInternal = (float) ($priceable->internal_cost ?? $priceable->base_price ?? 0);

        if (!method_exists($priceable, 'priceRules')) {
            return [
                'customer_price' => $baseCustomer,
                'internal_cost' => $baseInternal,
                'rule' => null,
            ];
        }

        $rules = $priceable->priceRules()
            ->where('is_active', true)
            ->orderBy('priority')
            ->orderByDesc('id')
            ->get();

        $selectedRule = null;
        foreach ($rules as $rule) {
            if (!$rule instanceof CatalogPriceRule) {
                continue;
            }
            if ($rule->appliesToDate($forDate)) {
                $selectedRule = $rule;
                break;
            }
        }

        if (!$selectedRule) {
            return [
                'customer_price' => $baseCustomer,
                'internal_cost' => $baseInternal,
                'gift_codes' => $priceable instanceof SerenataPackage ? array_values($priceable->gift_codes ?? []) : null,
                'artist_codes' => $priceable instanceof SerenataPackage ? array_values($priceable->artist_codes ?? []) : null,
                'other_cost_codes' => $priceable instanceof SerenataPackage ? array_values($priceable->other_cost_codes ?? []) : null,
                'rule' => null,
            ];
        }

        $resolvedGiftCodes = $priceable instanceof SerenataPackage
            ? (is_null($selectedRule->gift_codes) ? array_values($priceable->gift_codes ?? []) : array_values($selectedRule->gift_codes ?? []))
            : null;
        $resolvedArtistCodes = $priceable instanceof SerenataPackage
            ? (is_null($selectedRule->artist_codes) ? array_values($priceable->artist_codes ?? []) : array_values($selectedRule->artist_codes ?? []))
            : null;
        $resolvedOtherCostCodes = $priceable instanceof SerenataPackage
            ? (is_null($selectedRule->other_cost_codes) ? array_values($priceable->other_cost_codes ?? []) : array_values($selectedRule->other_cost_codes ?? []))
            : null;

        return [
            'customer_price' => $selectedRule->customer_price !== null ? (float) $selectedRule->customer_price : $baseCustomer,
            'internal_cost' => $selectedRule->internal_cost !== null ? (float) $selectedRule->internal_cost : $baseInternal,
            'gift_codes' => $resolvedGiftCodes,
            'artist_codes' => $resolvedArtistCodes,
            'other_cost_codes' => $resolvedOtherCostCodes,
            'rule' => $selectedRule,
        ];
    }
}
