<?php

namespace App\Models;

use App\Enums\CustomerType;
use App\Enums\OrderStatus;
use App\Enums\PaymentMethod;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

class Order extends Model
{
    use HasFactory;
    use SoftDeletes;
    use LogsActivity;

    protected $fillable = [
        'order_number',
        'customer_type',
        'user_id',
        'guest_customer_id',
        'customer_name',
        'customer_phone',
        'delivery_address_snapshot',
        'assigned_admin_id',
        'delivery_area_id',
        'delivery_cost',
        'voucher_id',
        'discount_amount',
        'subtotal',
        'grand_total',
        'status',
        'payment_method',
        'customer_notes',
        'admin_notes',
        'payment_deadline',
    ];

    protected function casts(): array
    {
        return [
            'customer_type'    => CustomerType::class,
            'delivery_cost'    => 'decimal:2',
            'discount_amount'  => 'decimal:2',
            'subtotal'         => 'decimal:2',
            'grand_total'      => 'decimal:2',
            'status'           => OrderStatus::class,
            'payment_method'   => PaymentMethod::class,
            'payment_deadline' => 'datetime',
        ];
    }

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly(['status', 'assigned_admin_id', 'admin_notes'])
            ->logOnlyDirty()
            ->useLogName('order');
    }

    // ── Relationships ─────────────────────────────────────────────────────

    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class);
    }

    public function guestCustomer(): BelongsTo
    {
        return $this->belongsTo(GuestCustomer::class);
    }

    public function assignedAdmin(): BelongsTo
    {
        return $this->belongsTo(User::class, 'assigned_admin_id');
    }

    public function deliveryArea(): BelongsTo
    {
        return $this->belongsTo(DeliveryArea::class);
    }

    public function voucher(): BelongsTo
    {
        return $this->belongsTo(Voucher::class);
    }

    public function items(): HasMany
    {
        return $this->hasMany(OrderItem::class);
    }

    public function payment(): HasOne
    {
        return $this->hasOne(Payment::class);
    }

    // ── Helpers ───────────────────────────────────────────────────────────

    public function isGuest(): bool
    {
        return $this->customer_type === CustomerType::Guest;
    }

    public function isPending(): bool
    {
        return $this->status === OrderStatus::Pending;
    }

    public function isCancellable(): bool
    {
        return $this->status->isCancellable();
    }

    public function isFinal(): bool
    {
        return $this->status->isFinal();
    }

    // ── Scopes ────────────────────────────────────────────────────────────

    public function scopeForCustomer($query, int $userId)
    {
        return $query->where('user_id', $userId);
    }

    public function scopeWithStatus($query, OrderStatus $status)
    {
        return $query->where('status', $status);
    }

    public function scopeNeedingAttention($query)
    {
        return $query->whereIn('status', [
            OrderStatus::Pending->value,
            OrderStatus::Paid->value,
        ]);
    }
}
