<?php

namespace App\Models;

use App\Enums\ApprovalStatus;
use App\Enums\MediaClassification;
use App\Enums\MediaType;
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\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
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 PartnerMedia extends Model implements HasMedia
{
    use HasFactory, HasHashid, InteractsWithMedia, SoftDeletes;

    protected $guarded = ['id'];

    protected function casts(): array
    {
        return [
            'type' => MediaType::class,
            'classification' => MediaClassification::class,
            'company_id' => 'integer',
        ];
    }

    #[Scope]
    protected function verified(Builder $query): void
    {
        $query->whereHas('company.verificationRequest', function ($q) {
            $q->approved();
        });
    }

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

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

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

    public static function dataChangeEntryLabels(): array
    {
        return [
            'name' => 'Nama',
            'link' => 'Tautan',
            'address' => 'Alamat',
            'type' => 'Jenis',
            'classification' => 'Klasifikasi',
            'journalism_organization' => 'No. Organisasi Kewartawanan',
            'journalism_organization_docs' => 'Dokumen Organisasi Kewartawanan',
            'press_council_certificate' => 'No. Sertifikat Dewan Pers',
            'press_council_certificate_docs' => 'Dokumen Sertifikat Dewan Pers',
        ];
    }

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

    public function announcements(): BelongsToMany
    {
        return $this->belongsToMany(Announcement::class, 'announcement_media');
    }

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

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

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

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

    public function dataChangeRequests(): MorphMany
    {
        return $this->morphMany(DataChangeRequest::class, 'entity');
    }

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

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

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