<?php

namespace App\Campaigns;

enum PerformanceVerdict: string
{
    case NoAction = 'no_action';
    case Continue = 'continue';
    case Pause = 'pause';
    case Stop = 'stop';
    case Start = 'start';
    case ActionRequired = 'action_required';

    public function label(): string
    {
        return match ($this) {
            self::NoAction => 'No action',
            self::Continue => 'Continue',
            self::Pause => 'Pause',
            self::Stop => 'Stop',
            self::Start => 'Start',
            self::ActionRequired => 'Action required',
        };
    }

    /**
     * Lower number wins when combining window verdicts.
     */
    public function priority(): int
    {
        return match ($this) {
            self::ActionRequired => 1,
            self::Stop => 2,
            self::Pause => 3,
            self::Start => 4,
            self::Continue => 5,
            self::NoAction => 6,
        };
    }

    public function isHardKill(): bool
    {
        return in_array($this, [self::Pause, self::Stop, self::ActionRequired], true);
    }

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

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

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

        return match ($value) {
            'wait_learning' => self::NoAction,
            'keep_running' => self::Continue,
            'investigate' => self::ActionRequired,
            default => null,
        };
    }

    public function statusClass(): string
    {
        return match ($this) {
            self::NoAction => 'queued',
            self::Continue => 'completed',
            self::Start => 'run',
            self::Pause, self::Stop, self::ActionRequired => 'failed',
        };
    }

    public function actionSummary(): string
    {
        return match ($this) {
            self::NoAction => 'No change recommended yet. Keep monitoring while the campaign gathers data.',
            self::Continue => 'Keep the campaign running. Performance looks acceptable for now.',
            self::Pause => 'Pause this campaign on the ad platform when you are ready.',
            self::Stop => 'Stop this campaign. Spend or performance is outside acceptable limits.',
            self::Start => 'Start or re-enable this campaign on the ad platform.',
            self::ActionRequired => 'Review this campaign soon. Something needs your attention.',
        };
    }
}
