<?php

namespace App\Models;

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

class EasypaisaCustomer extends Model
{
    use HasFactory, SoftDeletes;

    protected $primaryKey = 'customer_id';

    protected $fillable = [
        // Easypaisa identity
        'msisdn',
        'open_id',
        'auth_id',
        'union_id',
        'customer_eful_number',

        // Personal info
        'full_name',
        'cnic',
        'cnic_issue_date',
         'cnic_expiry_date',
        'dob',
        'gender',
        'marital_status',

        // Contact & address
        'email',
        'address',
        'city',
        'province',
        'country',
        'postal_code',

        // Geo
        'latitude',
        'longitude',

        // Media
        'user_avatar',

        // Status & verification
        'is_active',
        'is_verified',
        'is_easypaisa_verified',
        'easypaisa_verified_at',
        'cnic_verified_at',
        'last_login_at',

        // Device
        'device_token',
        'device_type',
        'app_version',

        // Physical card
        'card_status',
        'card_delivered',
        'card_requested_at',
        'card_dispatched_at',
        'card_delivered_at',

        // Delivery address
        'delivery_full_name',
        'delivery_mobile',
        'delivery_address',
        'delivery_city',
        'delivery_province',
        'delivery_postal_code',
        'courier_tracking_number',
    ];

    protected $casts = [
        'dob'                    => 'date',
        'is_active'              => 'boolean',
        'is_verified'            => 'boolean',
        'is_easypaisa_verified'  => 'boolean',
        'easypaisa_verified_at'  => 'datetime',
        'cnic_verified_at'       => 'datetime',
        'cnic_issue_date'  => 'date',
        'cnic_expiry_date' => 'date',
        'last_login_at'          => 'datetime',
        'latitude'               => 'decimal:7',
        'longitude'              => 'decimal:7',
        'card_delivered'         => 'boolean',
        'card_requested_at'      => 'datetime',
        'card_dispatched_at'     => 'datetime',
        'card_delivered_at'      => 'datetime',
        'created_at'             => 'datetime',
        'updated_at'             => 'datetime',
    ];

    // Hide sensitive fields from JSON output by default
    protected $hidden = [
        'auth_id',
        'device_token',
        'open_id',
        'union_id',
    ];

    // ── Scopes ─────────────────────────────────────────────────────────────
    public function scopeActive($query)
    {
        return $query->where('is_active', true);
    }

    public function scopeVerified($query)
    {
        return $query->where('is_verified', true);
    }

    // ── Accessors ──────────────────────────────────────────────────────────
    public function getMaskedCnicAttribute(): string
    {
        // Show only last 4 digits: XXXXX-XXXXXXX-X → *****-***1234-*
        return preg_replace('/\d(?=\d{4})/', '*', $this->cnic);
    }

    public function getMaskedMsisdnAttribute(): string
    {
        // 03XX-XXXXXXX → 03XX-***XXXX
        return substr($this->msisdn, 0, 4) . '****' . substr($this->msisdn, -3);
    }

    public function getAgeAttribute(): ?int
    {
        return $this->dob ? $this->dob->age : null;
    }
}