<?php

namespace App\Models\Hotel;

use Illuminate\Database\Eloquent\Model;

use App\Enums\Hotel\BehaviorReport;
use App\Enums\Hotel\CustomerSegment;

class Client extends Model
{
  //* Associated Table & Primary Key
    protected $table = 'clients';
    protected $primaryKey = 'id';

  //* Fillable Fields
    protected $fillable = [
      // Basic Data
        'client_name',
        'client_phone',
        'client_email',
        'client_state',

      // Identity Document Data
        'identity_document_type',
        'identity_document_number',

      // Behavior Control
        'customer_segment',
        'behavior_report',

      // Loyalty Data
        'discount_per_loyalty',

      // Tax Data
        'is_moral',
        'legal_name',
        'tax_system',
        'tax_id',
        "cfdi_use",

        'email_billing',
        'zip',
        'tax_address',
        'is_foreign_client',
        'foreign_tax_id',
        'is_blocked_for_billing',
        'facturapi_customer_id',
    ];

  //* Cast Columns
    protected $casts = [
      //* Behavior Data
        'customer_segment' => CustomerSegment::class,
        'behavior_report' => BehaviorReport::class,

      //* Tax Data
        'tax_address' => 'array',   // Example of structure on resources\json\modules\invoices\tax_address_structure.json

        'is_moral' => 'boolean',
        'is_foreign_client' => 'boolean',
        'is_blocked_for_billing' => 'boolean',
    ];

  //* Appends
    protected $appends = [
        // Segment
        'segment',
        'segment_label',
        'segment_badge',
        'segment_color',

        // Behavior
        'behavior_risk_score',
        'behavior_risk_level',
        'behavior_risk_color',
        'behavior_risk_level_label',
    ];

    // Segment Functions
    public function getSegmentAttribute(): ?array
    {
        if (!$this->customer_segment) return null;

        return [
            'value' => $this->customer_segment->value,
            'label' => $this->customer_segment->label(),
            'badge' => $this->customer_segment->badgeClass(),
            'color' => $this->customer_segment->color(),
        ];
    }

    public function getSegmentLabelAttribute(): ?string
    {
        return $this->customer_segment?->label();
    }

    public function getSegmentBadgeAttribute(): ?string
    {
        return $this->customer_segment?->badgeClass();
    }

    public function getSegmentColorAttribute(): ?string
    {
        return $this->customer_segment?->color();
    }

    // Behavior Functions
    public function getBehaviorRiskScoreAttribute(): ?int
    {
        return $this->behavior_report?->riskScore();
    }

    public function getBehaviorRiskLevelAttribute(): ?string
    {
        return $this->behavior_report?->riskLevel();
    }

    public function getBehaviorRiskColorAttribute(): ?string
    {
        return $this->behavior_report?->riskColor();
    }

    public function getBehaviorRiskLevelLabelAttribute(): ?string
    {
        return $this->behavior_report?->riskLevelLabel();
    }

  //* Relationships
    public function reservations() {
        return $this->hasMany(Reservation::class);
    }

    public function externalReservations() {
        return $this->hasMany(\App\Models\Modules\OtaConnections\ExternalReservation::class, "client_id");
    }

    public function taxInvoices() {
        return $this->hasMany(\App\Models\Invoicing\TaxInvoice::class);
    }

    public function taxPayments() {
        return $this->hasMany(\App\Models\Invoicing\TaxPayment::class);
    }

  //* External Relationships
    public function taxSystem()
    {
        return $this->belongsTo(\App\Models\Static\SAT\CatalogItem::class, 'tax_system', 'item_key')
            ->where('catalog_code', 'c_RegimenFiscal');
    }

    public function cfdiUse()
    {
        return $this->belongsTo(\App\Models\Static\SAT\CatalogItem::class, 'cfdi_use', 'item_key')
            ->where('catalog_code', 'c_UsoCFDI');
    }

}
