<?php

namespace App\Enums;

use App\Traits\ProvidesEnumOptions;
use InvalidArgumentException;

enum CuttingStatus: string
{
    use ProvidesEnumOptions;

    case IN_PROGRESS = 'in_progress';
    case COMPLETED = 'completed';
    case PENDING_VERIFICATION = 'pending_verification';
    case VERIFIED = 'verified';
    case REJECTED = 'rejected';

    public function label(): string
    {
        return match ($this) {
            self::IN_PROGRESS => 'Proses',
            self::COMPLETED => 'Selesai',
            self::PENDING_VERIFICATION => 'Menunggu Verifikasi',
            self::VERIFIED => 'Terverifikasi',
            self::REJECTED => 'Ditolak',
        };
    }

    public function isEditable(): bool
    {
        return in_array($this, [self::IN_PROGRESS, self::REJECTED], true);
    }

    public function transitionStatusMessage(): string
    {
        return match ($this) {
            self::COMPLETED => 'Proses cutting berhasil diselesaikan. Menunggu verifikasi admin toko.',
            self::VERIFIED => 'Proses cutting berhasil diverifikasi. Stok produk telah diperbarui.',
            self::REJECTED => 'Proses cutting berhasil ditolak.',
            self::IN_PROGRESS => 'Proses cutting dikembalikan ke proses.',
            default => 'Status proses cutting berhasil diperbarui.',
        };
    }

    public function canTransitionTo(self $status): bool
    {
        return match ($this) {
            self::IN_PROGRESS => $status === self::COMPLETED,
            self::COMPLETED => in_array($status, [self::PENDING_VERIFICATION, self::REJECTED, self::IN_PROGRESS, self::VERIFIED], true),
            self::PENDING_VERIFICATION => in_array($status, [self::VERIFIED, self::REJECTED, self::COMPLETED], true),
            self::REJECTED => $status === self::IN_PROGRESS,
            default => false,
        };
    }

    public function transitionPermission(): Permission
    {
        return match ($this) {
            self::COMPLETED => Permission::CUTTINGS_COMPLETE,
            self::PENDING_VERIFICATION => Permission::CUTTINGS_VERIFY,
            self::VERIFIED => Permission::CUTTINGS_VERIFY,
            self::REJECTED => Permission::CUTTINGS_REJECT,
            self::IN_PROGRESS => Permission::CUTTINGS_UPDATE,
            default => throw new InvalidArgumentException('Status tidak mendukung transisi.'),
        };
    }

    /**
     * @return list<array{status: string, label: string, destructive: bool, permission: string, icon_only: bool}>
     */
    public function availableActions(): array
    {
        return match ($this) {
            self::IN_PROGRESS => [
                [
                    'status' => self::COMPLETED->value,
                    'label' => 'Selesai',
                    'destructive' => false,
                    'permission' => Permission::CUTTINGS_COMPLETE->value,
                    'icon_only' => false,
                ],
            ],
            self::COMPLETED => [
                [
                    'status' => self::VERIFIED->value,
                    'label' => 'Verifikasi',
                    'destructive' => false,
                    'permission' => Permission::CUTTINGS_VERIFY->value,
                    'icon_only' => false,
                ],
            ],
            self::PENDING_VERIFICATION => [],
            self::REJECTED => [
                [
                    'status' => self::IN_PROGRESS->value,
                    'label' => 'Kembalikan ke Proses',
                    'destructive' => false,
                    'permission' => Permission::CUTTINGS_UPDATE->value,
                    'icon_only' => false,
                ],
            ],
            default => [],
        };
    }
}
