<?php

namespace App\Models;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Laravel\Sanctum\HasApiTokens;

class Customer extends Authenticatable
{
    use HasApiTokens;

    protected $guarded = [];

    // Casting agar tipe data konsisten saat diolah di Controller
    protected $casts = [
        'is_active' => 'boolean',
        'is_pkp' => 'boolean', 
        'credit_limit_active' => 'boolean',
        'multiple_invoice_hold_active' => 'boolean',
    ];

    // Relasi ke tabel Cabang
    public function branch(): BelongsTo
    {
        return $this->belongsTo(Branch::class);
    }

    // Relasi ke tabel Salesman
    public function salesman(): BelongsTo
    {
        return $this->belongsTo(Salesman::class, 'salesman_id', 'id');
    }

    // Relasi ke tabel District
    public function district(): BelongsTo
    {
        return $this->belongsTo(District::class);
    }

    // Relasi ke tabel CustomerType
    public function customerType() 
    {
        return $this->belongsTo(\App\Models\CustomerType::class, 'customer_type_id');
    }

    // --- TAMBAHAN: Helper untuk URL Foto KTP ---
    public function getKtpPhotoUrlAttribute()
    {
        return $this->ktp_photo_path ? asset('storage/' . $this->ktp_photo_path) : null;
    }
}