<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\HourRegistration\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Maestro\Core\Modules\HourRegistration\Enums\HourWorkflowStatus;
use Maestro\Core\Support\AuthUserResolver;
use Spatie\Activitylog\Models\Concerns\LogsActivity;
use Spatie\Activitylog\Support\LogOptions;

class HourTimesheetWeek extends Model
{
    use LogsActivity;

    protected $table = 'hour_timesheet_weeks';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'user_id',
        'week_start',
        'workflow_status',
        'submitted_at',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'week_start' => 'date',
            'workflow_status' => HourWorkflowStatus::class,
            'submitted_at' => 'datetime',
        ];
    }

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults()
            ->logOnly(['workflow_status', 'submitted_at'])
            ->logOnlyDirty()
            ->dontLogEmptyChanges()
            ->useLogName('hours');
    }

    /**
     * @return BelongsTo<Model, HourTimesheetWeek>
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(AuthUserResolver::eloquentModelClass(), 'user_id');
    }

    /**
     * @return HasMany<HourTimesheetLine, HourTimesheetWeek>
     */
    public function lines(): HasMany
    {
        return $this->hasMany(HourTimesheetLine::class, 'hour_timesheet_week_id');
    }
}
