<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Event extends Model
{
    use HasFactory;

    protected $table = 'adminlte_events';

    protected $fillable = [
        'user_id',
        'title',
        'start_at',
        'end_at',
        'all_day',
        'color',
    ];

    protected $casts = [
        'start_at' => 'datetime',
        'end_at' => 'datetime',
        'all_day' => 'boolean',
    ];

    public function user(): BelongsTo
    {
        return $this->belongsTo({{ users_model_ref }}::class, 'user_id');
    }

    /**
     * Format the event for FullCalendar's JSON feed.
     *
     * @return array<string, mixed>
     */
    public function toCalendarArray(): array
    {
        return [
            'id' => $this->id,
            'title' => $this->title,
            'start' => $this->start_at?->toIso8601String(),
            'end' => $this->end_at?->toIso8601String(),
            'allDay' => $this->all_day,
            'backgroundColor' => $this->color,
            'borderColor' => $this->color,
        ];
    }
}
