<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class AttendanceSummary extends Model
{
    protected $table = 'attendance_summaries';

    protected $fillable = [
        'user_id',
        'attendance_id',
        'action',
        'log_date',
        'log_time',
        'type',
        'status',
        'details',
        'source_id',
        'period_month',
        'transferred_at',
    ];

    protected $casts = [
        'log_date' => 'date',
        'period_month' => 'date',
        'transferred_at' => 'datetime',
        'details' => 'array',
    ];

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

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

    public function source(): BelongsTo
    {
        return $this->belongsTo(ActivityRecord::class, 'source_id');
    }

    /**
     * Get action label in Indonesian
     */
    public function getActionLabelAttribute(): string
    {
        return match($this->action) {
            'create' => 'Dibuat',
            'update' => 'Diperbarui',
            'delete' => 'Dihapus',
            'check_in' => 'Check In',
            'check_out' => 'Check Out',
            default => $this->action,
        };
    }

    /**
     * Scope for filtering by period
     */
    public function scopeForPeriod($query, $periodMonth)
    {
        return $query->where('period_month', $periodMonth);
    }
}
