<?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\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Illuminate\Database\Eloquent\Relations\Pivot;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
use Swindon\FilamentHashids\Traits\HasHashid;

class CooperationMedia extends Pivot implements HasMedia
{
    use HasFactory, HasHashid, InteractsWithMedia;

    protected $table = 'cooperation_media';

    protected $guarded = ['id'];

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

    #[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 payment(): HasOne
    {
        return $this->hasOne(CooperationPayment::class, 'cooperation_media_id');
    }

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