<?php

namespace App\Models;

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

class ServiceLog extends Model
{
    /**
     * Tabel ini hanya punya kolom created_at, tanpa updated_at.
     */
    public $timestamps = false;

    protected $fillable = [
        'ticket_id',
        'user_id',
        'note',
        'created_at',
    ];

    protected $casts = [
        'created_at' => 'datetime',
    ];

    // ─────────────────────── Boot ───────────────────────

    protected static function booted(): void
    {
        static::creating(function (ServiceLog $log) {
            if (empty($log->created_at)) {
                $log->created_at = now();
            }
        });
    }

    // ─────────────────────── Relationships ───────────────────────

    /**
     * Tiket servis yang terkait dengan log ini.
     */
    public function ticket(): BelongsTo
    {
        return $this->belongsTo(ServiceTicket::class, 'ticket_id');
    }

    /**
     * User (teknisi / admin) yang mencatat log ini.
     */
    public function user(): BelongsTo
    {
        return $this->belongsTo(User::class, 'user_id');
    }
}
