<?php

namespace App\Enums;

enum OrderStatus: string
{
    case Pending    = 'pending';
    case Paid       = 'paid';
    case Processing = 'processing';
    case Delivering = 'delivering';
    case Completed  = 'completed';
    case Cancelled  = 'cancelled';

    /**
     * Label dalam Bahasa Indonesia untuk tampilan UI.
     */
    public function label(): string
    {
        return match ($this) {
            self::Pending    => 'Menunggu Pembayaran',
            self::Paid       => 'Sudah Dibayar',
            self::Processing => 'Sedang Diproses',
            self::Delivering => 'Sedang Diantar',
            self::Completed  => 'Selesai',
            self::Cancelled  => 'Dibatalkan',
        };
    }

    /**
     * Warna badge untuk Flux UI.
     */
    public function color(): string
    {
        return match ($this) {
            self::Pending    => 'yellow',
            self::Paid       => 'blue',
            self::Processing => 'purple',
            self::Delivering => 'cyan',
            self::Completed  => 'green',
            self::Cancelled  => 'red',
        };
    }

    /**
     * Nama icon Heroicons untuk tampilan UI.
     */
    public function icon(): string
    {
        return match ($this) {
            self::Pending    => 'clock',
            self::Paid       => 'banknotes',
            self::Processing => 'cog-6-tooth',
            self::Delivering => 'truck',
            self::Completed  => 'check-circle',
            self::Cancelled  => 'x-circle',
        };
    }

    /**
     * Status berikutnya yang diizinkan dalam alur normal.
     * Mencegah admin melakukan transisi status yang tidak logis.
     *
     * Alur normal: pending → paid → processing → delivering → completed
     * Cancel hanya bisa dari: pending atau paid
     */
    public function allowedTransitions(): array
    {
        return match ($this) {
            self::Pending    => [self::Paid, self::Cancelled],
            self::Paid       => [self::Processing, self::Cancelled],
            self::Processing => [self::Delivering],
            self::Delivering => [self::Completed],
            self::Completed  => [],
            self::Cancelled  => [],
        };
    }

    public function canTransitionTo(self $next): bool
    {
        return in_array($next, $this->allowedTransitions());
    }

    /**
     * Apakah order ini masih bisa dibatalkan.
     */
    public function isCancellable(): bool
    {
        return in_array($this, [self::Pending, self::Paid]);
    }

    /**
     * Apakah order ini sudah final (tidak bisa diubah lagi).
     */
    public function isFinal(): bool
    {
        return in_array($this, [self::Completed, self::Cancelled]);
    }

    /**
     * Array untuk dropdown / select di form.
     */
    public static function forSelect(): array
    {
        return array_map(fn ($case) => [
            'value' => $case->value,
            'label' => $case->label(),
        ], self::cases());
    }
}
