<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Carbon\Carbon;
use App\Models\Agency\Agency;
use App\Models\Definition\City;
use App\Models\Tour\Tour;
use App\Models\Tour\TourDate;
use App\Models\Product\Product;
use App\Models\Product\ProductType;

class TourSeeder extends Seeder
{
     public function run(): void
     {
          $agency = Agency::first();
          if (!$agency)
               return;

          $productType = ProductType::where('code', 'kultur-turu')
               ->orWhere('slug', 'kultur-turu')
               ->first();

          if (!$productType)
               return;

          $istanbul = City::where('city', 'like', '%stanbul%')->first() ?? City::first();

          $toursData = [
               [
                    'code' => '1000',
                    'name' => 'Kapadokya Rüyası (3 Gün 2 Gece)',
                    'slug' => 'kapadokya-ruyasi-3-gun-2-gece',
                    'description' => '<p>Peri bacalarının kalbinde unutulmaz bir yolculuk.</p>',
                    'short_description' => '3 günlük konaklamalı Kapadokya kültür turu.',
                    'days' => 3,
                    'nights' => 2,
                    'max_participants' => 30,
                    'min_participants' => 10,
                    'departure_city_id' => $istanbul->id,
                    'arrival_city_id' => $istanbul->id,
                    'tour_type' => 'domestic',
                    'is_active' => true,
                    'adult_price_for_product' => 4500.00, // Product tablosuna yazılacak sembolik fiyat
                    'currency' => 'TRY'
               ]
          ];

          foreach ($toursData as $data) {
               $tour = Tour::updateOrCreate(
                    ['slug' => $data['slug']],
                    [
                         'agency_id' => $agency->id,
                         'code' => $data['code'],
                         'name' => $data['name'],
                         'slug' => $data['slug'],
                         'description' => $data['description'],
                         'short_description' => $data['short_description'],
                         'days' => $data['days'],
                         'nights' => $data['nights'],
                         'max_participants' => $data['max_participants'],
                         'min_participants' => $data['min_participants'],
                         'departure_city_id' => $data['departure_city_id'],
                         'arrival_city_id' => $data['arrival_city_id'],
                         'tour_type' => $data['tour_type'],
                         'is_active' => $data['is_active'],
                         'duration' => $data['days'],
                    ]
               );

               // Product kaydı
               $product = Product::updateOrCreate(
                    ['productable_type' => Tour::class, 'productable_id' => $tour->id],
                    [
                         'agency_id' => $agency->id,
                         'product_type_id' => $productType->id,
                         'code' => $data['code'],
                         'name' => $data['name'],
                         'slug' => Product::generateUniqueSlug($data['name']),
                         'description' => $data['description'],
                         'base_price' => $data['adult_price_for_product'],
                         'currency' => $data['currency'],
                         'is_active' => true,
                         'is_bookable' => true
                    ]
               );

               $tour->update(['product_id' => $product->id]);

               // Tarih ekle
               TourDate::where('cultural_tour_id', $tour->id)->forceDelete();
               $currentDate = Carbon::now()->addDays(5);
               for ($i = 0; $i < 3; $i++) {
                    TourDate::create([
                         'cultural_tour_id' => $tour->id,
                         'start_date' => $currentDate->copy()->addDays($i * 14)->format('Y-m-d'),
                         'end_date' => $currentDate->copy()->addDays($i * 14 + 2)->format('Y-m-d'),
                         'adult_price' => 4500.00,
                         'child_price' => 3150.00,
                         'infant_price' => 0,
                         'currency' => 'TRY',
                         'remaining_capacity' => $data['max_participants'],
                         'total_capacity' => $data['max_participants'],
                         'status' => 'booking_open',
                         'is_active' => true,
                         'is_bookable' => true,
                         'is_published' => true,
                         'is_confirmed' => true,
                         'guarantee_departure' => true
                    ]);
               }
          }
          $this->command->info('Kültür turları başarıyla eklendi!');
     }
}
