<?php

declare(strict_types=1);

namespace Maestro\Core\Modules\HourRegistration\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Maestro\Core\Modules\Customers\Models\Customer;
use Maestro\Core\Modules\HourRegistration\Enums\HourLineApprovalStatus;
use Maestro\Core\Modules\HourRegistration\Enums\HourRegistrationType;
use Maestro\Core\Support\AuthUserResolver;
use Spatie\Activitylog\Models\Concerns\LogsActivity;
use Spatie\Activitylog\Support\LogOptions;

class HourTimesheetLine extends Model
{
    use LogsActivity;

    public const WEEKDAY_KEYS = ['mon', 'tue', 'wed', 'thu', 'fri', 'sat', 'sun'];

    protected $table = 'hour_timesheet_lines';

    /**
     * @var list<string>
     */
    protected $fillable = [
        'hour_timesheet_week_id',
        'registration_type',
        'internal_category_id',
        'customer_id',
        'hour_project_id',
        'hour_task_id',
        'duration_mon',
        'duration_tue',
        'duration_wed',
        'duration_thu',
        'duration_fri',
        'duration_sat',
        'duration_sun',
        'line_approval_status',
        'approved_by_id',
        'approved_at',
        'reject_reason',
        'description',
    ];

    /**
     * @return array<string, string>
     */
    protected function casts(): array
    {
        return [
            'registration_type' => HourRegistrationType::class,
            'line_approval_status' => HourLineApprovalStatus::class,
            'approved_at' => 'datetime',
        ];
    }

    public function getActivitylogOptions(): LogOptions
    {
        $days = [];
        foreach (self::WEEKDAY_KEYS as $d) {
            $days[] = 'duration_'.$d;
        }

        return LogOptions::defaults()
            ->logOnly([
                'registration_type',
                'internal_category_id',
                'customer_id',
                'hour_project_id',
                'hour_task_id',
                ...$days,
                'line_approval_status',
                'approved_by_id',
                'approved_at',
                'reject_reason',
                'description',
            ])
            ->logOnlyDirty()
            ->dontLogEmptyChanges()
            ->useLogName('hours');
    }

    /**
     * @return BelongsTo<HourTimesheetWeek, HourTimesheetLine>
     */
    public function week(): BelongsTo
    {
        return $this->belongsTo(HourTimesheetWeek::class, 'hour_timesheet_week_id');
    }

    /**
     * @return BelongsTo<HourInternalCategory, HourTimesheetLine>
     */
    public function internalCategory(): BelongsTo
    {
        return $this->belongsTo(HourInternalCategory::class, 'internal_category_id');
    }

    /**
     * @return BelongsTo<Customer, HourTimesheetLine>
     */
    public function customer(): BelongsTo
    {
        return $this->belongsTo(Customer::class);
    }

    /**
     * @return BelongsTo<HourProject, HourTimesheetLine>
     */
    public function hourProject(): BelongsTo
    {
        return $this->belongsTo(HourProject::class, 'hour_project_id');
    }

    /**
     * @return BelongsTo<HourTask, HourTimesheetLine>
     */
    public function hourTask(): BelongsTo
    {
        return $this->belongsTo(HourTask::class, 'hour_task_id');
    }

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