<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Room;
use App\Models\RoomType;
use App\Models\Booking;
use App\Models\BookingCustomer;
use App\Models\Receipt;
use Carbon\Carbon;

class ReceiptTestSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Create a room type if it doesn't exist
        $roomType = RoomType::firstOrCreate([
            'room_type' => 'Standard',
        ], [
            'description' => 'Standard room with basic amenities',
            'price_per_night' => 100.00,
        ]);

        // Create a room if it doesn't exist
        $room = Room::firstOrCreate([
            'room_no' => '101',
        ], [
            'room_type_id' => $roomType->room_type_id,
            'floor' => '1st Floor',
            'description' => 'Standard room with city view',
            'capacity' => '2',
            'amenities' => 'WiFi, TV, AC, Private Bathroom',
            'price_per_night' => 100.00,
            'room_status' => 'Available',
            'status' => false,
            'deleteStatus' => false,
        ]);

        // Create a booking
        $booking = Booking::create([
            'room_id' => $room->room_id,
            'number_of_customers' => 2,
            'price_per_night' => 100.00,
            'number_of_nights' => 3,
            'check_in' => Carbon::now()->format('Y-m-d'),
            'check_out' => Carbon::now()->addDays(3)->format('Y-m-d'),
            'total_price' => 300.00,
            'advance_payment' => 100.00,
            'due_amount' => 200.00,
            'remaining_price' => 200.00,
            'observations' => 'Test booking for receipt functionality',
            'booking_status' => 'Checked In',
            'checked_in_at' => Carbon::now(),
            'payment_status' => false,
        ]);

        // Create booking customers
        BookingCustomer::create([
            'booking_id' => $booking->booking_id,
            'document_number' => '123456789',
            'document_type' => 'Passport',
            'first_name' => 'John',
            'last_name' => 'Doe',
            'nationality' => 'American',
            'origin_city' => 'New York',
            'phone' => '+1234567890',
            'email' => 'john.doe@example.com',
        ]);

        BookingCustomer::create([
            'booking_id' => $booking->booking_id,
            'document_number' => '987654321',
            'document_type' => 'Passport',
            'first_name' => 'Jane',
            'last_name' => 'Doe',
            'nationality' => 'American',
            'origin_city' => 'New York',
            'phone' => '+1234567891',
            'email' => 'jane.doe@example.com',
        ]);

        // Create a receipt
        Receipt::create([
            'booking_id' => $booking->booking_id,
            'receipt_number' => Receipt::generateReceiptNumber(),
            'total_amount' => 300.00,
            'advance_payment' => 100.00,
            'due_amount' => 200.00,
            'payment_method' => 'Cash',
            'notes' => 'Test receipt for demonstration',
            'status' => 'Partial',
            'issued_at' => Carbon::now(),
        ]);

        $this->command->info('Test data created successfully!');
        $this->command->info('Booking ID: ' . $booking->booking_id);
        $this->command->info('Receipt Number: ' . $booking->receipt->receipt_number);
    }
}
