<?php

namespace Database\Seeders;

use App\Models\Listing;
use App\Models\TourRequest;
use App\Models\User;
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class TourRequestSeeder extends Seeder
{
    /**
     * Run the database seeds.
     */
    public function run(): void
    {
        // Hardcoded id'ler fresh seed'de FK hatası veriyordu; mevcut kayıtlardan seçilir.
        $firstListing = Listing::query()->published()->orderBy('id')->first();
        $lastListing = Listing::query()->published()->orderByDesc('id')->first();
        $user = User::query()->orderBy('id')->first();

        if (!$firstListing) {
            return;
        }

        $requests = [
            [
                'user_id' => $user?->id,
                'listing_id' => $firstListing->id,
                'tour_type' => 'in_person',
                'tour_date' => now()->addDays(2)->setTime(14, 0),
                'name' => 'Deneme',
                'email' => 'deneme@gmail.com',
                'phone' => '055353553',
                'content' => 'Görüşme isteği',
            ],
            [
                'user_id' => null,
                'listing_id' => ($lastListing ?? $firstListing)->id,
                'tour_type' => 'virtual',
                'tour_date' => now()->addDays(5)->setTime(11, 0),
                'name' => 'Misafir Ziyaretçi',
                'email' => 'guest@gmail.com',
                'phone' => null,
                'content' => null,
            ],
        ];

        TourRequest::truncate();

        foreach ($requests as $request) {
            TourRequest::create($request);
        }
    }
}
