<?php

namespace App\Models;

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

class PoliceGuestRegistration extends Model
{
    use HasFactory;

    protected $fillable = [
        'booking_id',
        'room_id',
        'arrival_date',
        'room_code',
        'guest_number',
        'document_type',
        'surname',
        'name',
        'nationality',
        'fiscal_code',
        'birth_date',
        'birth_place',
        'gender',
        'height_cm',
        'document_number',
        'document_issue_date',
        'document_expiry_date',
        'document_issuing_municipality',
        'document_photo_1_path',
        'document_photo_2_path',
        'text_file_path',
        'telegram_chat_id',
        'telegram_message_id',
        'raw_payload',
    ];

    protected function casts(): array
    {
        return [
            'arrival_date' => 'date',
            'birth_date' => 'date',
            'document_issue_date' => 'date',
            'document_expiry_date' => 'date',
            'guest_number' => 'integer',
            'height_cm' => 'integer',
            'raw_payload' => 'array',
        ];
    }

    public function booking()
    {
        return $this->belongsTo(Booking::class);
    }

    public function room()
    {
        return $this->belongsTo(Room::class);
    }

    public function fullName(): string
    {
        return trim($this->surname.' '.$this->name);
    }

    public function italianTextBlock(): string
    {
        return implode(PHP_EOL, [
            'Registrazione ospite - Hotel Pineta',
            'Giorno di arrivo: '.$this->arrival_date?->format('d.m.Y'),
            'Camera: '.$this->room_code,
            'Ospite numero: '.$this->guest_number,
            'Tipo documento: '.($this->document_type ?: 'Non indicato'),
            'Cognome: '.$this->surname,
            'Nome: '.$this->name,
            'Nazionalità: '.($this->nationality ?: 'Non indicata'),
            'Codice fiscale: '.($this->fiscal_code ?: 'Assente'),
            'Data di nascita: '.($this->birth_date?->format('d.m.Y') ?: 'Non indicata'),
            'Luogo di nascita: '.($this->birth_place ?: 'Non indicato'),
            'Sesso: '.($this->gender ?: 'Non indicato'),
            'Altezza: '.($this->height_cm ? $this->height_cm.' cm' : 'Non indicata'),
            'Numero documento: '.($this->document_number ?: 'Non indicato'),
            'Data di rilascio: '.($this->document_issue_date?->format('d.m.Y') ?: 'Non indicata'),
            'Data di scadenza: '.($this->document_expiry_date?->format('d.m.Y') ?: 'Non indicata'),
            'Comune di emissione: '.($this->document_issuing_municipality ?: 'Non indicato'),
        ]).PHP_EOL;
    }
}
