<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Kasbon extends Model
{
    use HasFactory;

    protected static function boot()
    {
        parent::boot();
        
        static::creating(function ($kasbon) {
            if ($kasbon->tanggal_kasbon) {
                $date = \Carbon\Carbon::parse($kasbon->tanggal_kasbon);
                $kasbon->periode_bulan = $date->month;
                $kasbon->periode_tahun = $date->year;
            }
            if (auth()->check()) {
                $kasbon->diinput_oleh = auth()->id();
            }
        });
    }

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'kasbons';

    /**
     * The primary key associated with the table.
     *
     * @var string
     */
    protected $primaryKey = 'id_kasbon';

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'id_teknisi',
        'jumlah_kasbon',
        'tanggal_kasbon',
        'periode_bulan',
        'periode_tahun',
        'keperluan',
        'keterangan_detail',
        'metode_pemberian',
        'diinput_oleh',
        'status',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'jumlah_kasbon' => 'decimal:2',
        'tanggal_kasbon' => 'date',
        'status' => 'string',
        'created_at' => 'timestamp',
        'updated_at' => 'timestamp',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [];

    /**
     * Status constants
     */
    const STATUS_LUNAS = 'lunas';
    const STATUS_BELUM_LUNAS = 'belum_lunas';

    /**
     * Get all available status options
     *
     * @return array
     */
    public static function getStatusOptions()
    {
        return [
            self::STATUS_LUNAS => 'Lunas',
            self::STATUS_BELUM_LUNAS => 'Belum Lunas'
        ];
    }

    /**
     * Scope untuk filter berdasarkan status
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @param string $status
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeByStatus($query, $status)
    {
        return $query->where('status', $status);
    }

    /**
     * Scope untuk kasbon yang belum lunas
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeBelumLunas($query)
    {
        return $query->where('status', self::STATUS_BELUM_LUNAS);
    }

    /**
     * Scope untuk kasbon yang sudah lunas
     *
     * @param \Illuminate\Database\Eloquent\Builder $query
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeLunas($query)
    {
        return $query->where('status', self::STATUS_LUNAS);
    }

    /**
     * Accessor untuk format jumlah kasbon dalam rupiah
     *
     * @return string
     */
    public function getJumlahKasbonFormatAttribute()
    {
        return 'Rp ' . number_format($this->jumlah_kasbon, 0, ',', '.');
    }

    /**
     * Accessor untuk format tanggal kasbon
     *
     * @return string
     */
    public function getTanggalKasbonFormatAttribute()
    {
        return $this->tanggal_kasbon ? $this->tanggal_kasbon->format('d/m/Y') : '';
    }

    /**
     * Accessor untuk status dalam bahasa Indonesia
     *
     * @return string
     */
    public function getStatusLabelAttribute()
    {
        $statusOptions = self::getStatusOptions();
        return $statusOptions[$this->status] ?? $this->status;
    }

    /**
     * Mutator untuk format jumlah kasbon sebelum disimpan
     *
     * @param mixed $value
     * @return void
     */
    public function setJumlahKasbonAttribute($value)
    {
        // Hapus format rupiah jika ada
        $cleanValue = str_replace(['Rp', '.', ',', ' '], '', $value);
        $this->attributes['jumlah_kasbon'] = (float) $cleanValue;
    }

    /**
     * Accessor untuk keterangan (alias keperluan)
     */
    public function getKeteranganAttribute()
    {
        return $this->keperluan;
    }

    /**
     * Relasi ke Teknisi
     */
    public function teknisi()
    {
        return $this->belongsTo(Teknisi::class, 'id_teknisi', 'id_teknisi');
    }
}