<?php

namespace App\Modules\Production\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\LogOptions;

class ProductionOrder extends Model
{
    use HasFactory, LogsActivity;

    // 狀態常數
    const STATUS_DRAFT = 'draft';
    const STATUS_PENDING = 'pending';
    const STATUS_APPROVED = 'approved';
    const STATUS_IN_PROGRESS = 'in_progress';
    const STATUS_COMPLETED = 'completed';
    const STATUS_CANCELLED = 'cancelled';

    protected $fillable = [
        'code',
        'product_id',
        'warehouse_id',
        'output_quantity',
        'output_batch_number',
        'output_box_count',
        'production_date',
        'expiry_date',
        'user_id',
        'status',
        'remark',
    ];

    /**
     * 檢查是否可以轉移至新狀態，並驗證權限。
     */
    public function canTransitionTo(string $newStatus, $user = null): bool
    {
        $user = $user ?? auth()->user();
        if (!$user) return false;
        if ($user->hasRole('super-admin')) return true;

        $currentStatus = $this->status;

        // 定義合法的狀態轉移路徑與所需權限
        $transitions = [
            self::STATUS_DRAFT => [
                self::STATUS_PENDING => 'production_orders.view', // 基本檢視者即可送審
                self::STATUS_CANCELLED => 'production_orders.cancel',
            ],
            self::STATUS_PENDING => [
                self::STATUS_APPROVED => 'production_orders.approve',
                self::STATUS_DRAFT => 'production_orders.approve', // 退回草稿
                self::STATUS_CANCELLED => 'production_orders.cancel',
            ],
            self::STATUS_APPROVED => [
                self::STATUS_IN_PROGRESS => 'production_orders.edit', // 啟動製作需要編輯權限
                self::STATUS_CANCELLED => 'production_orders.cancel',
            ],
            self::STATUS_IN_PROGRESS => [
                self::STATUS_COMPLETED => 'production_orders.edit', // 完成製作需要編輯權限
                self::STATUS_CANCELLED => 'production_orders.cancel',
            ],
        ];

        if (!isset($transitions[$currentStatus])) {
            return false;
        }

        if (!array_key_exists($newStatus, $transitions[$currentStatus])) {
            return false;
        }

        $requiredPermission = $transitions[$currentStatus][$newStatus];
        
        return $requiredPermission ? $user->can($requiredPermission) : true;
    }

    protected $casts = [
        'production_date' => 'date',
        'expiry_date' => 'date',
        'output_quantity' => 'decimal:2',
    ];

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly([
                'code',
                'status',
                'output_quantity',
                'output_batch_number',
                'production_date',
                'remark'
            ])
            ->logOnlyDirty()
            ->dontSubmitEmptyLogs()
            ->setDescriptionForEvent(fn(string $eventName) => "生產工單已{$this->getEventDescription($eventName)}");
    }

    protected function getEventDescription($eventName): string
    {
        return match ($eventName) {
            'created' => '建立',
            'updated' => '更新',
            'deleted' => '刪除',
            default => $eventName,
        };
    }

    public static function generateCode()
    {
        $prefix = 'PO' . now()->format('Ymd');
        $lastOrder = self::where('code', 'like', $prefix . '%')->latest()->first();
        if ($lastOrder) {
            $lastSequence = intval(substr($lastOrder->code, -3));
            $sequence = str_pad($lastSequence + 1, 3, '0', STR_PAD_LEFT);
        } else {
            $sequence = '001';
        }
        return $prefix . $sequence;
    }

    public function items(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(ProductionOrderItem::class);
    }
}
