<?php

namespace App\Models;

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

class LesenPenjaja extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id', 
        'syarikat_id', 
        'jenis', 
        'jenis_lain', 
        'status_tanah', 
        'status_lain', 
        'no_petak', 
        'kawasan_id', 
        'taman_id', 
        'jalan_id', 
        'penempatan_id', 
        'makanan', 
        'minuman', 
        'lain', 
        'masa_jualan_mula', 
        'masa_jualan_tamat', 
        'jenis_kenderaan', 
        'no_pendaftaran', 
        'longitude', 
        'latitude',
        'no_akaun_lesen', 
        'no_fail_lesen', 
        'kod_lesen',
        'jenis_perniagaan',
        'status_progress', 
        'status_mesyuarat', 
        'kodkawasan', 
        'kodtaman', 
        'mulajanji', 
        'tamatjanji', 
        'tarikhmohon', 
        'tarikhlulus',
        'dt_lesen_dikeluarkan',
        'kpi',
        'patuh_kpi'
    ];
    
    public function user()
    {
        return $this->belongsTo(User::class);
    }

    public function syarikat(){
        return $this->belongsTo(Syarikat::class);
    }

    public function kawasan(){
        return $this->belongsTo(Kawasan::class);
    }

    public function taman(){
        return $this->belongsTo(Taman::class);
    }

    public function jalan(){
        return $this->belongsTo(Jalan::class);
    }

    public function penempatan(){
        return $this->belongsTo(Penempatan::class);
    }

    public function dokumens()
    {
        return $this->belongsToMany(Dokumen::class, 'lesen_penjaja_dokumens', 'lesen_penjaja_id', 'dokumen_id')->withPivot('path', 'jenis_dokumen');
    }

    public function dokumenId($jenis_dokumen)
    {
        $exists = $this->dokumens()->where('nama', $jenis_dokumen)->first();

        return $exists ? $exists->id : null;
    
    }

    public function bil_pelbagai_api(){
        return $this->hasMany(BilPelbagaiApi::class);
    }

    public function bilpelbagais(){
        return $this->hasMany(BilPelbagai::class);
    }

    public function getDaysDifferenceAttribute()
    {
        return $this->tarikhmohon && $this->tarikhlulus
            ? Carbon::parse($this->tarikhmohon)->diffInDays(Carbon::parse($this->tarikhlulus))
            : null;
    }

    public function ulasanIk(){
        return $this->hasMany(BorangUlasanIk::class);
    }

    public function ulasanPegawai(){
        return $this->hasMany(UlasanPegawai::class);
    }

    public function pemeriksaanTerkini(){
         return $this->hasOne(BorangUlasanIK::class)->latestOfMany();
    }
    
    public function mesyuarats()
    {
        return $this->belongsToMany(MesyuaratPelesenan::class, 'mesyuarat_pelesenan_lesen_penjajas')
                    ->withPivot(['by_law'])
                    ->withPivot(['kodlesen'])
                    ->withPivot(['kadar_lesen'])
                    ->withPivot(['kadar_sampah'])
                    ->withPivot(['kadar_sewa_petak'])
                    ->withPivot(['kadar_patil'])
                    ->withPivot(['keputusan_mesyuarat'])
                    ->withTimestamps();
    }

    public function kiraKpi(): ?int
    {
        if (!$this->tarikhmohon || !$this->dt_lesen_dikeluarkan) {
            return null; // Atau 0 jika kau prefer
        }

        $mohon = Carbon::parse($this->tarikhmohon)->startOfDay();
        $keluar = Carbon::parse($this->dt_lesen_dikeluarkan)->startOfDay();

        return $mohon->diffInDays($keluar);
    }

    public function lokasi_penjajaan(){
        $alamat = '';
        if(!is_null($this->no_petak)){
            $alamat = ucwords($this->no_petak).", \n";
        }

        if(!is_null($this->penempatan?->nama)){
            $alamat .= ucwords($this->penempatan?->nama).", \n";
        }

        if(!is_null($this->jalan?->nama)){
            $alamat .= ucwords(strtolower($this->jalan?->nama)).", \n";
        }

        if(!is_null($this->taman?->nama)){
            $alamat .= ucwords(strtolower($this->taman?->nama)).", \n";
        }

        $alamat .= ucwords(strtolower($this->kawasan->nama));

        return $alamat;
    }

    public function jenis_penjaja()
    {
        return $this->belongsTo(JenisPenjaja::class);
    }

    public function jenis_jualan()
    {
        return $this->belongsTo(JenisJualan::class);
    }

    public function no_resit_wang_proses()
    {
        $bil = BilPelbagai::where('lesen_penjaja_id', $this->id)->where('jenis_bil','wang proses')->where('status_bil', true)->first();

        return $bil->no_resit;
    }

}
