<?php

namespace App\Models;

use App\Enums\PaymentProofReviewStatus;
use App\Support\PublicId;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class EventPaymentProof extends Model
{
    protected $fillable = [
        'public_id',
        'event_registration_id',
        'manual_payment_method_id',
        'file_path',
        'original_name',
        'mime_type',
        'size',
        'transfer_reference',
        'amount_minor',
        'user_notes',
        'review_status',
        'reviewed_by',
        'reviewed_at',
        'rejection_reason',
    ];

    public function getRouteKeyName(): string
    {
        return 'public_id';
    }

    protected function casts(): array
    {
        return [
            'size' => 'integer',
            'amount_minor' => 'integer',
            'review_status' => PaymentProofReviewStatus::class,
            'reviewed_at' => 'datetime',
        ];
    }

    protected static function booted(): void
    {
        static::creating(function (EventPaymentProof $proof): void {
            if (empty($proof->public_id)) {
                $proof->public_id = PublicId::generate();
            }
        });
    }

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

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

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