<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Database\Eloquent\Relations\MorphMany;
use Swindon\FilamentHashids\Traits\HasHashid;

class TaskAssignment extends Model
{
    use HasFactory, HasHashid;

    protected $guarded = ['id'];

    protected function casts(): array
    {
        return [
            'start_date' => 'date',
            'end_date' => 'date',
            'report_amount' => 'integer',
            'cooperation_id' => 'integer',
        ];
    }

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

    public function mediaTaskAssignments(): HasMany
    {
        return $this->hasMany(MediaTaskAssignment::class);
    }

    public function partnerMedia(): BelongsToMany
    {
        return $this->belongsToMany(PartnerMedia::class, 'media_task_assignment')
            ->using(MediaTaskAssignment::class)
            ->withPivot(['status'])
            ->withTimestamps();
    }

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

    public function reports(): HasManyThrough
    {
        return $this->hasManyThrough(
            Report::class,
            MediaTaskAssignment::class,
            'task_assignment_id',
            'media_task_assignment_id',
            'id',
            'id'
        );
    }
}
