<?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 Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Swindon\FilamentHashids\Traits\HasHashid;

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

    protected $guarded = ['id'];

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

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

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

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

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

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

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