<?php

namespace App\Models;

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

class EntradaTiempo extends Model
{
    use HasFactory;

    protected $table = 'entradas_tiempo';

    protected $fillable = [
        'hoja_tiempo_id', 'centro_costo_id', 'fecha', 'horas',
    ];

    protected $casts = [
        'fecha' => 'date',
        'horas' => 'decimal:2',
    ];

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

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

    public function comentarios(): HasMany
    {
        return $this->hasMany(ComentarioTiempo::class);
    }

    public function getDiaCalendarioAttribute(): ?Calendario
    {
        return Calendario::where('fecha', $this->fecha)->first();
    }
}
