<?php

namespace App\Models\Modules\OtaConnections;

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

use App\Traits\ChecksTableExistence;

class ExternalReservation extends Model
{
  //* Support & Extension
    use HasFactory;
    use ChecksTableExistence;

  //* Associated Table & Primary Key
    protected $table = 'ota_external_reservations';
    protected $primaryKey = 'id';

  //* Fillable
    protected $fillable = [
      //* Basic Data
        // Of Creation
        "reservation_date",
        "receptionist_id",

        // Of Registration
        "client_id",
        "client_reference",
        "entry_date",
        "entry_hour",
        "departure_date",
        "departure_hour",
        "number_nights",
        "number_adults",
        "number_children",
        "requiere_invoice",
        "reservation_reason",
        "notes",

        // Of Stay
        "room_type_id",
        "external_type_reference",
        "units_quantity",
        "reserved_rooms",

        // Of Contact
        "ota_connection_id",

        // Of Payment
        "final_price",
        "total_price",
        "lodging_contract",
        "consumption_contract",
        "extras_contract",

      //* Control Data
        "global_status_id",
        "has_checkIn",
        "has_checkOut",
        "show_in_calendar",
        "is_active",
        "payment_status",
    ];

  //* Cast Columns
    protected $casts = [
        'reserved_rooms' => 'array',    // Basic Structure: [1, 2, 3]
        'requiere_invoice' => 'boolean',
    ];

  //* Relationships
    // Of Creation
        public function user() {
            return $this->belongsTo(\App\Models\User::class, 'receptionist_id');
        }

    // Of Registration
        public function client() {
            return $this->belongsTo(\App\Models\Hotel\Client::class, 'client_id');
        }

    // Of Stay
        public function roomType() {
            return $this->belongsTo(\App\Models\Hotel\RoomType::class, 'room_type_id');
        }

    // Of Contact
        public function otaConnection() {
            return $this->belongsTo(\App\Models\Modules\OtaConnections\OTAConnection::class, 'ota_connection_id');
        }

    // Control Data
        public function status() {
            return $this->belongsTo(\App\Models\Hotel\Status::class, 'global_status_id');
        }

    // Polymorphic Relations
        public function taxInvoice() {
            return $this->morphOne(\App\Models\Invoicing\TaxInvoice::class, 'system_service');
        }
}
