<?php

namespace Modules\Activity\Entities;

use App\Traits\HasDocuments;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\Relations\MorphMany;

class Activity extends Model
{
    use HasDocuments, HasUuids;

    public const GALLERY_DOCUMENT_TYPE = 'gallery';

    protected $table = 'activities';

    protected $fillable = [
        'activity_type_id',
        'title',
        'reference_number',
        'description',
        'start_datetime',
        'end_datetime',
        'organizer',
        'location',
        'is_active',
    ];

    protected function casts(): array
    {
        return [
            'title' => 'string',
            'reference_number' => 'string',
            'description' => 'string',
            'start_datetime' => 'datetime',
            'end_datetime' => 'datetime',
            'organizer' => 'string',
            'location' => 'string',
            'is_active' => 'boolean',
        ];
    }

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

    public function reports(): HasMany
    {
        return $this->hasMany(ActivityReport::class)->orderByDesc('created_at');
    }

    public function galleryImages(): MorphMany
    {
        return $this->documents()->where('type', self::GALLERY_DOCUMENT_TYPE);
    }

    public function attachments(): MorphMany
    {
        return $this->documents()->where('type', '!=', self::GALLERY_DOCUMENT_TYPE);
    }
}
