<?php

namespace App\Models;

use App\Enums\CooperationStatus;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

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

    protected $guarded = ['id'];

    protected function casts(): array
    {
        return [
            'initial_submission_date' => 'date',
            'final_submission_date' => 'date',
            'assignment_deadline' => 'date',
            'verification_deadline' => 'date',
            'payment_amount' => 'integer',
            'payment_date' => 'date',
            'status' => CooperationStatus::class,
        ];
    }

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

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

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

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

    public function reports(): \Illuminate\Database\Eloquent\Relations\HasManyThrough
    {
        return $this->hasManyThrough(Report::class, TaskAssignment::class);
    }
}
