<?php

namespace App\Models;

use App\Enums\PaymentMethod;
use App\Enums\PaymentStatus;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;

class Payment extends Model
{
    use LogsActivity;

    protected $fillable = [
        'order_id',
        'method',
        'amount',
        'sender_bank',
        'sender_name',
        'proof_image',
        'status',
        'verified_by',
        'verified_at',
        'rejection_notes',
    ];

    protected function casts(): array
    {
        return [
            'amount'      => 'decimal:2',
            'method'      => PaymentMethod::class,
            'status'      => PaymentStatus::class,
            'verified_at' => 'datetime',
        ];
    }

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

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

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

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

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

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

    public function isVerified(): bool
    {
        return $this->status === PaymentStatus::Verified;
    }

    public function isRejected(): bool
    {
        return $this->status === PaymentStatus::Rejected;
    }
}
