<?php

namespace App\Models;

use App\Enums\FolderPriority;
use App\Enums\FolderStatus;
use App\Enums\FolderType;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\LogOptions;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;

class Folder extends Model implements HasMedia
{
    /** @use HasFactory<\Database\Factories\FolderFactory> */
    use HasFactory, InteractsWithMedia, LogsActivity, SoftDeletes;

    /**
     * The attributes that are mass assignable.
     *
     * @var list<string>
     */
    protected $fillable = [
        'workspace_id',
        'client_id',
        'created_by',
        'title',
        'type',
        'period_year',
        'period_month',
        'period_quarter',
        'due_date',
        'status',
        'priority',
        'assigned_to',
        'validated_at',
        'closed_at',
        'confirmation_requested_at',
        'confirmation_media_id',
        'confirmed_by_type',
        'confirmed_by_id',
        'confirmation_signature',
        'refused_at',
        'refusal_reason',
        'notes_internal',
        'notes_client',
        'created_at',
    ];

    /**
     * Get the attributes that should be cast.
     *
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'type' => FolderType::class,
            'status' => FolderStatus::class,
            'priority' => FolderPriority::class,
            'validated_at' => 'datetime',
            'closed_at' => 'datetime',
            'confirmation_requested_at' => 'datetime',
            'refused_at' => 'datetime',
            'due_date' => 'date',
        ];
    }

    /**
     * Register media collections.
     */
    public function registerMediaCollections(): void
    {
        $this->addMediaCollection('documents')->useDisk('local');
    }

    /**
     * Get the workspace that owns the folder.
     *
     * @return BelongsTo<Workspace, $this>
     */
    public function workspace(): BelongsTo
    {
        return $this->belongsTo(Workspace::class);
    }

    /**
     * Get the client that owns the folder.
     *
     * @return BelongsTo<Client, $this>
     */
    public function client(): BelongsTo
    {
        return $this->belongsTo(Client::class);
    }

    /**
     * Get the user who created the folder.
     *
     * @return BelongsTo<User, $this>
     */
    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * Get the user assigned to the folder.
     *
     * @return BelongsTo<User, $this>
     */
    public function assignee(): BelongsTo
    {
        return $this->belongsTo(User::class, 'assigned_to');
    }

    /**
     * Get the messages for the folder.
     *
     * @return HasMany<Message>
     */
    public function messages(): HasMany
    {
        return $this->hasMany(Message::class);
    }

    /**
     * Get the invitations for the folder.
     *
     * @return HasMany<FolderInvitation>
     */
    public function invitations(): HasMany
    {
        return $this->hasMany(FolderInvitation::class);
    }

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logFillable()
            ->logOnlyDirty()
            ->dontSubmitEmptyLogs();
    }
}
