<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class EventGuest extends Model
{
    protected $fillable = [
        'event_id',
        'name',
        'companion_names',
        'phone',
        'card_type',
        'total_attendees',
        'checkin_timestamps',
        'checked_in_count',
        'card_number',
        'channels',
        'status',
        'rsvp_status',
        'rsvp_responded_at',
        'rsvp_message',
        'generated_card_url',
        'card_sent_at',
        'whatsapp_sent_at',
        'whatsapp_delivered_at',
        'whatsapp_read_at',
        'whatsapp_failed_at',
        'whatsapp_status',
        'whatsapp_message_id',
        'whatsapp_message',
        'whatsapp_error',
        'whatsapp_received_at',
        'whatsapp_received_message',
        'reminder_sent_at',
        'thankyou_sent_at',
        'sms_card_count',
        'sms_reminder_count',
        'sms_thankyou_count',
        'sms_welcome_count',
        'sms_contribution_count',
        'whatsapp_card_count',
        'whatsapp_reminder_count',
        'whatsapp_thankyou_count',
        'whatsapp_welcome_count',
        'whatsapp_contribution_count',
        'whatsapp_contribution_card_count',
        'sms_failed_at',
        'sms_error',
        'checked_in_at',
        'first_checkin_at',
        'second_checkin_at',
        'third_checkin_at',
        'fourth_checkin_at',
        'reply_message',
        // Scanner / check-in fields added by scan migration
        'qr_code',
        'checked_in',
        'checked_in_by',
        'check_in_method',
        'check_in_count',
        'last_check_in_at',
        'scanner_notes',
        'scanner_device',
    ];

    protected $casts = [
        'channels' => 'array',
        'card_sent_at' => 'datetime',
        'reminder_sent_at' => 'datetime',
        'thankyou_sent_at' => 'datetime',
        'sms_failed_at' => 'datetime',
        'checked_in_at' => 'datetime',
        'first_checkin_at' => 'datetime',
        'second_checkin_at' => 'datetime',
        'third_checkin_at' => 'datetime',
        'fourth_checkin_at' => 'datetime',
        'rsvp_responded_at' => 'datetime',
        'whatsapp_sent_at' => 'datetime',
        'whatsapp_delivered_at' => 'datetime',
        'whatsapp_read_at' => 'datetime',
        'whatsapp_failed_at' => 'datetime',
        'whatsapp_received_at' => 'datetime',
        'checked_in_count' => 'integer',
        'total_attendees' => 'integer',
    ];

    /**
     * Get the event that owns the guest.
     */
    public function event()
    {
        return $this->belongsTo(Event::class);
    }

    /**
     * Check if guest is fully checked in
     */
    public function isFullyCheckedIn(): bool
    {
        $checkedInCount = $this->checked_in_count ?? 0;
        $totalAttendees = $this->total_attendees ?? 1;
        return $checkedInCount >= $totalAttendees;
    }

    /**
     * Get the last check-in timestamp
     */
    public function getLastCheckInAttribute()
    {
        $count = $this->checked_in_count ?? 0;
        
        if ($count >= 4 && $this->fourth_checkin_at) {
            return $this->fourth_checkin_at;
        }
        if ($count >= 3 && $this->third_checkin_at) {
            return $this->third_checkin_at;
        }
        if ($count >= 2 && $this->second_checkin_at) {
            return $this->second_checkin_at;
        }
        if ($count >= 1 && $this->first_checkin_at) {
            return $this->first_checkin_at;
        }
        
        return null;
    }

    /**
     * Generate QR code value from card number
     */
    public function getQrCodeAttribute()
    {
        return $this->card_number;
    }
}
