<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class MediaItem extends Model
{
    use SoftDeletes;

    protected $guarded = ['id', 'created_at', 'updated_at'];

    protected $casts = [
        'files' => 'array',
        'tags' => 'array',
        'start_at' => 'datetime',
        'end_at' => 'datetime',
        'created_at_file' => 'datetime',
        'is_draft' => 'boolean',
        'gps_lat' => 'float',
        'gps_lng' => 'float',
    ];

    public const TYPE_IMAGE = 'image';
    public const TYPE_VIDEO = 'video';
    public const TYPE_DOCUMENT = 'document';

    public const UPLOADER_ADMIN = 'admin';
    public const UPLOADER_PRODUCER = 'producer';
    public const UPLOADER_EMPLOYER = 'employer';

    public const STATUS_PENDING = 'pending';
    public const STATUS_APPROVED = 'approved';
    public const STATUS_REJECTED = 'rejected';
    public const STATUS_NEEDS_EDIT = 'needs_edit';

    public static function typeOptions(): array
    {
        return [
            self::TYPE_IMAGE => 'تصویر',
            self::TYPE_VIDEO => 'ویدیو',
            self::TYPE_DOCUMENT => 'سند',
        ];
    }

    public static function uploaderTypeOptions(): array
    {
        return [
            self::UPLOADER_ADMIN => 'ادمین',
            self::UPLOADER_PRODUCER => 'تولیدکننده',
            self::UPLOADER_EMPLOYER => 'کارفرما',
        ];
    }

    public static function statusOptions(): array
    {
        return [
            self::STATUS_PENDING => 'انتظار',
            self::STATUS_APPROVED => 'تایید',
            self::STATUS_REJECTED => 'رد',
            self::STATUS_NEEDS_EDIT => 'ویرایش',
        ];
    }

    protected static function booted(): void
    {
        static::addGlobalScope('order', function (Builder $q) {
            $q->orderByDesc('created_at');
        });
    }

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

    public function category(): BelongsTo
    {
        return $this->belongsTo(MediaCategory::class, 'category_id');
    }

    public function subcategory(): BelongsTo
    {
        return $this->belongsTo(MediaCategory::class, 'subcategory_id');
    }

    public function createdByUser(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /** منبع آپلودکننده برای نمایش (ادمین/تولیدکننده/کارفرما از روی uploader_type و uploader_id) */
    public function getUploaderDisplayAttribute(): string
    {
        return self::uploaderTypeOptions()[$this->uploader_type] ?? $this->uploader_type;
    }

    public function comments(): MorphMany
    {
        return $this->morphMany(Comment::class, 'commentable');
    }

    public function ratings(): MorphMany
    {
        return $this->morphMany(Rating::class, 'rateable');
    }

    public function flags(): \Illuminate\Database\Eloquent\Relations\HasMany
    {
        return $this->hasMany(MediaFlag::class);
    }
}
