<?php

namespace App\Models;

use App\Enums\ApprovalStatus;
use Illuminate\Database\Eloquent\Attributes\Scope;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Swindon\FilamentHashids\Traits\HasHashid;

class Report extends Model implements HasMedia
{
    use HasFactory, HasHashid, InteractsWithMedia, SoftDeletes;

    protected $guarded = ['id'];

    protected function casts(): array
    {
        return [
            'publication_date' => 'date',
            'status' => ApprovalStatus::class,
            'media_task_assignment_id' => 'integer',
        ];
    }

    #[Scope]
    protected function accepted(Builder $query): void
    {
        $query->where('status', ApprovalStatus::ACCEPTED);
    }

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

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

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

    public function rejectionReasons(): MorphMany
    {
        return $this->morphMany(RejectionReason::class, 'rejectable');
    }
}
