<?php

namespace App\Enums;

use App\Traits\ProvidesEnumOptions;
use InvalidArgumentException;

enum OrderStatus: string
{
    use ProvidesEnumOptions;

    case PENDING = 'pending';
    case PROCESSING = 'processing';
    case COMPLETED = 'completed';
    case CANCELLED = 'cancelled';

    public function label(): string
    {
        return match ($this) {
            self::PENDING => 'Menunggu',
            self::PROCESSING => 'Diproses',
            self::COMPLETED => 'Selesai',
            self::CANCELLED => 'Dibatalkan',
        };
    }

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

    public function transitionStatusMessage(): string
    {
        return match ($this) {
            self::PROCESSING => 'Pesanan berhasil dikirim.',
            self::COMPLETED => 'Pesanan berhasil diselesaikan.',
            self::CANCELLED => 'Pesanan berhasil dibatalkan.',
            default => 'Status pesanan berhasil diperbarui.',
        };
    }

    public function canTransitionTo(self $status): bool
    {
        return match ($this) {
            self::PENDING => in_array($status, [self::PROCESSING, self::CANCELLED], true),
            self::PROCESSING => in_array($status, [self::COMPLETED, self::CANCELLED], true),
            self::COMPLETED, self::CANCELLED => false,
        };
    }

    public function transitionPermission(): Permission
    {
        return match ($this) {
            self::PROCESSING => Permission::ORDERS_SEND,
            self::COMPLETED => Permission::ORDERS_COMPLETE,
            self::CANCELLED => Permission::ORDERS_CANCEL,
            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::PENDING => [
                [
                    'status' => self::PROCESSING->value,
                    'label' => 'Kirim',
                    'destructive' => false,
                    'permission' => Permission::ORDERS_SEND->value,
                    'icon_only' => true,
                ],
                [
                    'status' => self::CANCELLED->value,
                    'label' => 'Batalkan',
                    'destructive' => true,
                    'permission' => Permission::ORDERS_CANCEL->value,
                    'icon_only' => true,
                ],
            ],
            self::PROCESSING => [
                [
                    'status' => self::COMPLETED->value,
                    'label' => 'Selesai',
                    'destructive' => false,
                    'permission' => Permission::ORDERS_COMPLETE->value,
                    'icon_only' => true,
                ],
                [
                    'status' => self::CANCELLED->value,
                    'label' => 'Batalkan',
                    'destructive' => true,
                    'permission' => Permission::ORDERS_CANCEL->value,
                    'icon_only' => true,
                ],
            ],
            default => [],
        };
    }
}
