<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Support\Str;

class CampBooking extends Model
{
    protected $fillable = [
        'parent_name',
        'parent_email',
        'parent_phone',
        'child_name',
        'child_dob',
        'child_gender',
        'special_needs',
        'location', // Add this location
        'status',
        'camp_package_id',
        'payment_reference'
    ];

    protected $casts = [
        'child_dob' => 'date',
    ];

    public function camp_package(): BelongsTo
    {
        return $this->belongsTo(CampPackage::class);
    }

    public function getFormattedChildDobAttribute(): string
    {
        return $this->child_dob ? $this->child_dob->format('d M Y') : '';
    }

    protected static function booted()
    {
        static::creating(function ($booking) {
            if (empty($booking->payment_reference)) {
                $booking->payment_reference = $booking->generatePaymentReference();
            }
        });
    }

    public function generatePaymentReference(): string
    {
        $date = now()->format('Ymd');
        $random = Str::upper(Str::random(6));
        return "BE-$date-$random";
    }
}
