<?php

namespace App\Modules\Procurement\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;


class PurchaseOrder extends Model
{
    /** @use HasFactory<\Database\Factories\PurchaseOrderFactory> */
    use HasFactory;
    use \Spatie\Activitylog\Traits\LogsActivity;

    protected $fillable = [
        'code',
        'vendor_id',
        'warehouse_id',
        'user_id',
        'order_date',
        'expected_delivery_date',
        'status',
        'total_amount',
        'tax_amount',
        'grand_total',
        'remark',
    ];

    protected $casts = [
        'order_date' => 'date',
        'expected_delivery_date' => 'date',
        'total_amount' => 'decimal:2',
    ];

    public function getActivitylogOptions(): \Spatie\Activitylog\LogOptions
    {
        return \Spatie\Activitylog\LogOptions::defaults()
            ->logAll()
            ->logOnlyDirty()
            ->dontSubmitEmptyLogs();
    }

    public function tapActivity(\Spatie\Activitylog\Contracts\Activity $activity, string $eventName)
    {
        $snapshot = $activity->properties['snapshot'] ?? [];
        
        $snapshot['po_number'] = $this->code;
        
        if ($this->vendor) {
            $snapshot['vendor_name'] = $this->vendor->name;
        }
        // Warehouse relation removed in Strict Mode. Snapshot should be set via manual hydration if needed, 
        // or during the procurement process where warehouse_id is known.

        $activity->properties = $activity->properties->merge([
            'snapshot' => $snapshot
        ]);
    }

    public function vendor(): \Illuminate\Database\Eloquent\Relations\BelongsTo
    {
        return $this->belongsTo(Vendor::class);
    }





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

    /**
     * 檢查是否可以轉移至新狀態，並驗證權限。
     */
    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 = [
            'draft' => [
                'pending' => 'purchase_orders.view', // 基本檢視者即可送審
                'cancelled' => 'purchase_orders.cancel',
            ],
            'pending' => [
                'approved' => 'purchase_orders.approve',
                'draft' => 'purchase_orders.approve', // 退回草稿
                'cancelled' => 'purchase_orders.cancel',
            ],
            'approved' => [
                'cancelled' => 'purchase_orders.cancel',
                'partial' => null, // 系統自動轉移，不需手動權限點
            ],
            'partial' => [
                'completed' => null, // 系統自動轉移
                'closed' => 'purchase_orders.approve', // 手動結案通常需要核准權限
                'cancelled' => 'purchase_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;
    }
}
