<?php

namespace App\Models;

use App\Enums\DataChangeStatus;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\SoftDeletes;
use Swindon\FilamentHashids\Traits\HasHashid;

class DataChangeRequest extends Model
{
    use HasHashid, SoftDeletes;

    protected $guarded = ['id'];

    protected function casts(): array
    {
        return [
            'status' => DataChangeStatus::class,
            'old_data' => 'array',
            'new_data' => 'array',
            'user_id' => 'integer',
            'entity_id' => 'integer',
        ];
    }

    #[Scope]
    protected function pending(Builder $query): void
    {
        $query->where('status', DataChangeStatus::PENDING);
    }

    #[Scope]
    protected function approved(Builder $query): void
    {
        $query->where('status', DataChangeStatus::APPROVED);
    }

    #[Scope]
    protected function rejected(Builder $query): void
    {
        $query->where('status', DataChangeStatus::REJECTED);
    }

    public function entity(): MorphTo
    {
        return $this->morphTo();
    }

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

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