<?php

namespace App\Library\CampaignDecision;

use App\Campaigns\PerformanceVerdict;

enum CampaignDecisionVerdict: string
{
    case InsufficientData = 'insufficient_data';
    case DataIssue = 'data_issue';
    case Learning = 'learning';
    case Watch = 'watch';
    case Maintain = 'maintain';
    case ScaleUp = 'scale_up';
    case DecreaseBudget = 'decrease_budget';
    case Pause = 'pause';

    public function label(): string
    {
        return match ($this) {
            self::InsufficientData => 'Insufficient data',
            self::DataIssue => 'Data issue',
            self::Learning => 'Learning',
            self::Watch => 'Watch',
            self::Maintain => 'Maintain',
            self::ScaleUp => 'Scale up',
            self::DecreaseBudget => 'Decrease budget',
            self::Pause => 'Pause',
        };
    }

    public function priority(): int
    {
        return match ($this) {
            self::DataIssue => 1,
            self::Pause => 2,
            self::DecreaseBudget => 3,
            self::ScaleUp => 4,
            self::Learning => 5,
            self::Watch => 6,
            self::Maintain => 7,
            self::InsufficientData => 8,
        };
    }

    public function statusClass(): string
    {
        return match ($this) {
            self::Maintain, self::ScaleUp => 'completed',
            self::Learning, self::InsufficientData, self::Watch => 'queued',
            self::DataIssue, self::Pause, self::DecreaseBudget => 'failed',
        };
    }

    public function needsAttention(): bool
    {
        return in_array($this, [
            self::DataIssue,
            self::Pause,
            self::DecreaseBudget,
            self::Watch,
        ], true);
    }

    public function actionSummary(): string
    {
        return match ($this) {
            self::InsufficientData => 'Not enough data yet. Keep monitoring.',
            self::DataIssue => 'Fix data or sync issues before changing the campaign.',
            self::Learning => 'Campaign is learning. Avoid aggressive changes.',
            self::Watch => 'Mixed signals. Review again on the next hourly run.',
            self::Maintain => 'Performance looks acceptable. No budget change recommended.',
            self::ScaleUp => 'Consider increasing budget within guardrail limits.',
            self::DecreaseBudget => 'Consider reducing budget within guardrail limits.',
            self::Pause => 'Consider pausing on the ad platform when ready.',
        };
    }

    public static function tryFromStored(?string $value): ?self
    {
        if ($value === null || $value === '') {
            return null;
        }

        $verdict = self::tryFrom($value);

        if ($verdict !== null) {
            return $verdict;
        }

        $legacy = PerformanceVerdict::tryFromStored($value);

        return match ($legacy) {
            PerformanceVerdict::Pause, PerformanceVerdict::Stop => self::Pause,
            PerformanceVerdict::Continue => self::Maintain,
            PerformanceVerdict::ActionRequired => self::DataIssue,
            PerformanceVerdict::NoAction => self::InsufficientData,
            PerformanceVerdict::Start => self::Watch,
            default => null,
        };
    }
}
