<?php
/**
 * Generates barbershop_import.sql for empty MySQL 8 DB (schema + RealDataSeeder data).
 * Run: php scripts/export_seed_sql.php > ../database/barbershop_import.sql
 * Or:  php scripts/export_seed_sql.php (writes to database/barbershop_import.sql)
 */

declare(strict_types=1);

$outFile = dirname(__DIR__).'/database/barbershop_import.sql';

$toE164 = function (string $raw): string {
    if (str_starts_with($raw, '506')) {
        return '+'.$raw;
    }
    return '+506'.$raw;
};

$clients = [
    '85334909' => 'Maikel', '62203282' => 'Moisés', '83892496' => 'Kevin', '88065891' => 'Esteban',
    '84814368' => 'Mariana', '72066890' => 'Gabriel', '61376323' => 'Román', '83231477' => 'Wilfredo',
    '88412370' => 'Isaac', '86693131' => 'José', '88468622' => 'David', '87395555' => 'Edgardo',
    '89632442' => 'Melisa', '83444644' => 'Ronald', '88327508' => 'Cesar', '88394857' => 'Seth',
    '89495154' => 'Randall', '84450305' => 'Eduardo', '50660945196' => 'Rick', '88451795' => 'Juan',
    '83610121' => 'Kerlin', '83440201' => 'Augusto', '88236658' => 'Jason', '89108154' => 'Oscar',
    '88459062' => 'German', '88113356' => 'Olivier', '85491602' => 'Erik', '89528420' => 'Josue',
    '87800479' => 'Diego', '88804002' => 'Carlos', '61609725' => 'Diogenes', '84881229' => 'Eder',
    '87070343' => 'Yirlany', '83101824' => 'Carmen', '83285085' => 'Michael', '87061385' => 'Fabricio',
    '86796911' => 'Monse', '89237070' => 'Alex', '70160184' => 'Alonso', '88291243' => 'Sergio',
    '84684782' => 'Jorge', '89156698' => 'Rosvin', '83441007' => 'Dr', '88939195' => 'Herbert',
    '71130542' => 'Mauricio', '87706395' => 'Lukas', '84956272' => 'Manfreth', '60217777' => 'Saul',
    '60296926' => 'Sebastian', '85333737' => 'Savier', '84559701' => 'Yeraldin', '85671072' => 'Mainor',
    '83135334' => 'Ignacio', '88238233' => 'Gerardo', '83470443' => 'Rocio', '85607667' => 'Jeremy',
    '88143400' => 'Ramón', '89393464' => 'Karol', '84928358' => 'Karla', '86537781' => 'María',
    '64167201' => 'Deiver', '83088106' => 'José M', '87084415' => 'Kevin A', '88235288' => 'Edgardo A',
    '88316910' => 'Michael A', '88884682' => 'Juan C', '86816085' => 'Jorge A', '83332873' => 'Carlos A',
    '86945640' => 'Carlos B', '84603574' => 'Carlos C', '83135029' => 'José L', '84684751' => 'Jose R',
    '87298490' => 'Jennifer', '87044008' => 'José G', '83902207' => 'Olga', '83217479' => 'Miguel',
    '87114826' => 'Mauricio B', '70189926' => 'Diego A',
];

$pinHashes = [];
foreach ($clients as $raw => $_) {
    $d = preg_replace('/\D/', '', (string) $raw);
    $pin = substr($d, -4);
    if (! isset($pinHashes[$pin])) {
        $pinHashes[$pin] = password_hash($pin, PASSWORD_BCRYPT);
    }
}
$hashPassword = password_hash('password', PASSWORD_BCRYPT);

$esc = static fn (string $s): string => str_replace(["\0", "\n", "\r", '\\', "'", '"'], ["\\0", "\\n", "\\r", '\\\\', "\\'", '\\"'], $s);

$now = (new DateTimeImmutable('now', new DateTimeZone('UTC')))->format('Y-m-d H:i:s');

ob_start();

echo "-- Barbería San Ramón: schema + seed for empty MySQL 8\n";
echo "-- Generated UTC: {$now}\n";
echo "-- Login admin: admin@barbershop.com / password\n";
echo "-- Login barbero Maikel: 85334909 o +50685334909 / password\n";
echo "-- Clientes: teléfono (8 dígitos o 506...) / PIN = últimos 4 del número en la lista del seeder\n";
echo "-- Regenerar este archivo: php laravel-base/scripts/export_seed_sql.php\n";
echo "SET NAMES utf8mb4 COLLATE utf8mb4_unicode_ci;\n";
echo 'SET FOREIGN_KEY_CHECKS=0;'."\n\n";

$drops = [
    'appointment_photos', 'appointments', 'barber_blocks', 'barber_services', 'custom_service_logs',
    'push_subscriptions', 'whatsapp_conversations', 'failed_jobs', 'job_batches', 'jobs',
    'cache_locks', 'cache', 'personal_access_tokens', 'barbers', 'services', 'users', 'migrations',
];
foreach ($drops as $t) {
    echo "DROP TABLE IF EXISTS `{$t}`;\n";
}
echo "\n";

echo <<<'SQL'
CREATE TABLE `users` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `role` enum('admin','barber','client') NOT NULL DEFAULT 'client',
  `phone` varchar(20) DEFAULT NULL,
  `avatar_url` varchar(500) DEFAULT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT '1',
  `remember_token` varchar(100) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_email_unique` (`email`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `barbers` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `user_id` bigint unsigned NOT NULL,
  `bio` text,
  `specialty` varchar(100) DEFAULT NULL,
  `working_days` json DEFAULT NULL,
  `work_start` time NOT NULL,
  `work_end` time NOT NULL,
  `display_order` tinyint NOT NULL DEFAULT '0',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `barbers_user_id_foreign` (`user_id`),
  CONSTRAINT `barbers_user_id_foreign` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `services` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `category` varchar(100) DEFAULT NULL,
  `description` text,
  `duration_minutes` smallint NOT NULL,
  `price` decimal(8,2) NOT NULL,
  `is_active` tinyint(1) NOT NULL DEFAULT '1',
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `barber_services` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `barber_id` bigint unsigned NOT NULL,
  `service_id` bigint unsigned NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `barber_services_barber_id_service_id_unique` (`barber_id`,`service_id`),
  KEY `barber_services_service_id_foreign` (`service_id`),
  CONSTRAINT `barber_services_barber_id_foreign` FOREIGN KEY (`barber_id`) REFERENCES `barbers` (`id`) ON DELETE CASCADE,
  CONSTRAINT `barber_services_service_id_foreign` FOREIGN KEY (`service_id`) REFERENCES `services` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `appointments` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `client_id` bigint unsigned NOT NULL,
  `barber_id` bigint unsigned NOT NULL,
  `service_id` bigint unsigned NOT NULL,
  `appointment_date` date NOT NULL,
  `start_time` time NOT NULL,
  `end_time` time NOT NULL,
  `status` enum('pending','confirmed','completed','cancelled') NOT NULL DEFAULT 'pending',
  `notes` text,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `appointments_client_id_foreign` (`client_id`),
  KEY `appointments_barber_id_appointment_date_status_index` (`barber_id`,`appointment_date`,`status`),
  KEY `appointments_service_id_foreign` (`service_id`),
  CONSTRAINT `appointments_barber_id_foreign` FOREIGN KEY (`barber_id`) REFERENCES `barbers` (`id`) ON DELETE CASCADE,
  CONSTRAINT `appointments_client_id_foreign` FOREIGN KEY (`client_id`) REFERENCES `users` (`id`) ON DELETE CASCADE,
  CONSTRAINT `appointments_service_id_foreign` FOREIGN KEY (`service_id`) REFERENCES `services` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `appointment_photos` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `appointment_id` bigint unsigned NOT NULL,
  `barber_id` bigint unsigned NOT NULL,
  `file_path` varchar(500) NOT NULL,
  `file_size_kb` smallint NOT NULL,
  `taken_at` timestamp NOT NULL,
  `notes` varchar(500) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `appointment_photos_appointment_id_foreign` (`appointment_id`),
  KEY `appointment_photos_barber_id_foreign` (`barber_id`),
  CONSTRAINT `appointment_photos_appointment_id_foreign` FOREIGN KEY (`appointment_id`) REFERENCES `appointments` (`id`) ON DELETE CASCADE,
  CONSTRAINT `appointment_photos_barber_id_foreign` FOREIGN KEY (`barber_id`) REFERENCES `barbers` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `barber_blocks` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `barber_id` bigint unsigned NOT NULL,
  `block_date` date NOT NULL,
  `start_time` time NOT NULL,
  `end_time` time NOT NULL,
  `reason` varchar(200) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `barber_blocks_barber_id_foreign` (`barber_id`),
  CONSTRAINT `barber_blocks_barber_id_foreign` FOREIGN KEY (`barber_id`) REFERENCES `barbers` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `personal_access_tokens` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `tokenable_type` varchar(255) NOT NULL,
  `tokenable_id` bigint unsigned NOT NULL,
  `name` text NOT NULL,
  `token` varchar(64) NOT NULL,
  `abilities` text,
  `last_used_at` timestamp NULL DEFAULT NULL,
  `expires_at` timestamp NULL DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `personal_access_tokens_token_unique` (`token`),
  KEY `personal_access_tokens_tokenable_type_tokenable_id_index` (`tokenable_type`,`tokenable_id`),
  KEY `personal_access_tokens_expires_at_index` (`expires_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `custom_service_logs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `barber_id` bigint unsigned NOT NULL,
  `client_name` varchar(200) DEFAULT NULL,
  `client_id` bigint unsigned DEFAULT NULL,
  `description` text NOT NULL,
  `service_date` date NOT NULL,
  `start_time` time NOT NULL,
  `end_time` time NOT NULL,
  `price` decimal(10,2) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `custom_service_logs_barber_id_service_date_index` (`barber_id`,`service_date`),
  KEY `custom_service_logs_client_id_foreign` (`client_id`),
  CONSTRAINT `custom_service_logs_barber_id_foreign` FOREIGN KEY (`barber_id`) REFERENCES `barbers` (`id`) ON DELETE CASCADE,
  CONSTRAINT `custom_service_logs_client_id_foreign` FOREIGN KEY (`client_id`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `push_subscriptions` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `subscribable_type` varchar(255) NOT NULL,
  `subscribable_id` bigint unsigned NOT NULL,
  `endpoint` varchar(500) NOT NULL,
  `public_key` varchar(255) DEFAULT NULL,
  `auth_token` varchar(255) DEFAULT NULL,
  `content_encoding` varchar(20) DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `push_subs_sub_endpoint_unique` (`subscribable_type`,`subscribable_id`,`endpoint`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `whatsapp_conversations` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `phone` varchar(20) NOT NULL,
  `state` varchar(50) NOT NULL DEFAULT 'menu',
  `data` json DEFAULT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `whatsapp_conversations_phone_unique` (`phone`),
  KEY `whatsapp_conversations_phone_index` (`phone`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `cache` (
  `key` varchar(255) NOT NULL,
  `value` mediumtext NOT NULL,
  `expiration` int NOT NULL,
  PRIMARY KEY (`key`),
  KEY `cache_expiration_index` (`expiration`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `cache_locks` (
  `key` varchar(255) NOT NULL,
  `owner` varchar(255) NOT NULL,
  `expiration` int NOT NULL,
  PRIMARY KEY (`key`),
  KEY `cache_locks_expiration_index` (`expiration`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `jobs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `queue` varchar(255) NOT NULL,
  `payload` longtext NOT NULL,
  `attempts` tinyint unsigned NOT NULL,
  `reserved_at` int unsigned DEFAULT NULL,
  `available_at` int unsigned NOT NULL,
  `created_at` int unsigned NOT NULL,
  PRIMARY KEY (`id`),
  KEY `jobs_queue_reserved_at_available_at_index` (`queue`,`reserved_at`,`available_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `job_batches` (
  `id` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `total_jobs` int NOT NULL,
  `pending_jobs` int NOT NULL,
  `failed_jobs` int NOT NULL,
  `failed_job_ids` longtext NOT NULL,
  `options` mediumtext,
  `cancelled_at` int DEFAULT NULL,
  `created_at` int NOT NULL,
  `finished_at` int DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `failed_jobs` (
  `id` bigint unsigned NOT NULL AUTO_INCREMENT,
  `uuid` varchar(255) NOT NULL,
  `connection` text NOT NULL,
  `queue` text NOT NULL,
  `payload` longtext NOT NULL,
  `exception` longtext NOT NULL,
  `failed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `failed_jobs_uuid_unique` (`uuid`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `migrations` (
  `id` int unsigned NOT NULL AUTO_INCREMENT,
  `migration` varchar(255) NOT NULL,
  `batch` int NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

SQL;

// --- users ---
$userRows = [];
$e164ToUserId = [];
$nextId = 1;

$userRows[] = "({$nextId},'Admin','admin@barbershop.com','{$esc($hashPassword)}','admin',NULL,NULL,1,NULL,'{$now}','{$now}')";
$e164ToUserId['admin@barbershop.com'] = $nextId;
$nextId++;

$maikelE164 = '+50685334909';
$userRows[] = "({$nextId},'Maikel','{$esc($maikelE164)}','{$esc($hashPassword)}','barber','{$esc($maikelE164)}',NULL,1,NULL,'{$now}','{$now}')";
$e164ToUserId[$maikelE164] = $nextId;
$maikelUserId = $nextId;
$nextId++;

foreach ($clients as $raw => $name) {
    $e164 = $toE164((string) $raw);
    if (isset($e164ToUserId[$e164])) {
        continue;
    }
    $d = preg_replace('/\D/', '', (string) $raw);
    $pin = substr($d, -4);
    $pw = $pinHashes[$pin];
    $userRows[] = "({$nextId},'{$esc($name)}','{$esc($e164)}','{$esc($pw)}','client','{$esc($e164)}',NULL,1,NULL,'{$now}','{$now}')";
    $e164ToUserId[$e164] = $nextId;
    $nextId++;
}

$firstClientId = $maikelUserId;
foreach ($clients as $raw => $_) {
    $e164 = $toE164((string) $raw);
    if (! isset($e164ToUserId[$e164])) {
        continue;
    }
    $uid = $e164ToUserId[$e164];
    if ($uid === 1 || $uid === $maikelUserId) {
        continue;
    }
    $firstClientId = $uid;
    break;
}

echo 'INSERT INTO `users` (`id`,`name`,`email`,`password`,`role`,`phone`,`avatar_url`,'
    ."`is_active`,`remember_token`,`created_at`,`updated_at`) VALUES\n"
    .implode(",\n", $userRows).";\n\n";

$resolveClient = function (string $rawPhone) use ($toE164, $e164ToUserId, $firstClientId): int {
    $e164 = $toE164($rawPhone);
    if (isset($e164ToUserId[$e164])) {
        return $e164ToUserId[$e164];
    }

    return $firstClientId;
};

// --- barber ---
echo "INSERT INTO `barbers` (`id`,`user_id`,`bio`,`specialty`,`working_days`,`work_start`,`work_end`,`display_order`,`created_at`,`updated_at`) VALUES\n";
echo "(1,{$maikelUserId},NULL,'Corte y Barba','[1,2,3,4,5,6]','06:00:00','20:00:00',1,'{$now}','{$now}');\n\n";

$services = [
    ['Corte', null, null, 30, 8000.00],
    ['Corte y Barba con Vapor', null, null, 60, 15000.00],
    ['Corte y Barba Rápido', null, null, 30, 10000.00],
    ['Corte y Barba Premium', null, null, 60, 18000.00],
    ['Corte y Barba con Corte de Hijo', null, null, 90, 20000.00],
    ['Corte Pixie', null, null, 60, 12000.00],
    ['Corte Rápido', null, null, 20, 6000.00],
    ['Barba', null, null, 30, 6000.00],
    ['Padre e Hijo', null, null, 60, 15000.00],
    ['Corte Cabello Medio', null, null, 60, 10000.00],
];
$svcLines = [];
$i = 1;
foreach ($services as $s) {
    [$n, $c, $d, $dur, $pr] = $s;
    $svcLines[] = "({$i},'{$esc($n)}',NULL,NULL,{$dur},".number_format($pr, 2, '.', '').",1,'{$now}','{$now}')";
    $i++;
}
echo 'INSERT INTO `services` (`id`,`name`,`category`,`description`,`duration_minutes`,`price`,`is_active`,`created_at`,`updated_at`) VALUES'."\n";
echo implode(",\n", $svcLines).";\n\n";

$bs = [];
for ($bid = 1; $bid <= 10; $bid++) {
    $bs[] = "({$bid},1,{$bid})";
}
echo "INSERT INTO `barber_services` (`id`,`barber_id`,`service_id`) VALUES\n".implode(",\n", $bs).";\n\n";

$serviceIds = [];
foreach ($services as $idx => $s) {
    $serviceIds[$s[0]] = $idx + 1;
}
$map = [
    'CORTE Y BARBA CON CORTE DE HIJO' => 'Corte y Barba con Corte de Hijo',
    'CORTE Y BARBA CON VAPOR' => 'Corte y Barba con Vapor',
    'CORTE Y BARBA RÁPIDO' => 'Corte y Barba Rápido',
    'CORTE Y BARBA RAPIDO' => 'Corte y Barba Rápido',
    'CORTE Y BARBA PREMIUM' => 'Corte y Barba Premium',
    'CORTE PIXIE' => 'Corte Pixie',
    'CORTE RÁPIDO' => 'Corte Rápido',
    'CORTE RAPIDO' => 'Corte Rápido',
    'CORTE CABELLO MEDIO' => 'Corte Cabello Medio',
    'PADRE E HIJO' => 'Padre e Hijo',
    'BARBA' => 'Barba',
    'CORTE' => 'Corte',
];
$resolveService = function (string $raw) use ($serviceIds, $map): int {
    $key = strtoupper(trim($raw));
    $name = $map[$key] ?? 'Corte';

    return $serviceIds[$name];
};

$durations = [];
for ($j = 1; $j <= 10; $j++) {
    $durations[$j] = $services[$j - 1][3];
}

$appointments = include dirname(__DIR__).'/scripts/appointment_rows.php';

$apptLines = [];
$aid = 1;
foreach ($appointments as $appt) {
    [$year, $month, $day, $hour, $minute, $serviceName, $phone] = $appt;
    $serviceId = $resolveService($serviceName);
    $clientId = $resolveClient((string) $phone);
    $dur = $durations[$serviceId];
    $startTime = sprintf('%02d:%02d:00', $hour, $minute);
    $dt = new DateTimeImmutable(sprintf('%04d-%02d-%02d %s', $year, $month, $day, $startTime));
    $endTime = $dt->modify("+{$dur} minutes")->format('H:i:s');
    $apptDate = sprintf('%04d-%02d-%02d', $year, $month, $day);
    $isPast = $dt < new DateTimeImmutable('now');
    $status = $isPast ? 'completed' : 'confirmed';
    $apptLines[] = "({$aid},{$clientId},1,{$serviceId},'{$apptDate}','{$startTime}','{$endTime}','{$status}',NULL,'{$now}','{$now}')";
    $aid++;
}

echo 'INSERT INTO `appointments` (`id`,`client_id`,`barber_id`,`service_id`,`appointment_date`,`start_time`,`end_time`,`status`,`notes`,`created_at`,`updated_at`) VALUES'."\n";
echo implode(",\n", $apptLines).";\n\n";

$migrationBatch = 1;
$migrationFiles = [
    '0001_01_01_000001_create_cache_table',
    '0001_01_01_000002_create_jobs_table',
    '2024_01_01_000001_create_users_table',
    '2024_01_01_000002_create_barbers_table',
    '2024_01_01_000003_create_services_table',
    '2024_01_01_000004_create_barber_services_table',
    '2024_01_01_000005_create_appointments_table',
    '2024_01_01_000006_create_appointment_photos_table',
    '2024_01_01_000007_create_barber_blocks_table',
    '2024_01_01_000008_add_category_to_services_table',
    '2024_01_01_000009_create_custom_service_logs_table',
    '2024_01_01_000010_create_push_subscriptions_table',
    '2024_01_01_000011_create_whatsapp_conversations_table',
    '2026_03_11_092816_create_personal_access_tokens_table',
];
$mLines = [];
$mid = 1;
foreach ($migrationFiles as $m) {
    $mLines[] = "({$mid},'{$m}',{$migrationBatch})";
    $mid++;
}
echo "INSERT INTO `migrations` (`id`, `migration`, `batch`) VALUES\n".implode(",\n", $mLines).";\n\n";

echo "ALTER TABLE `users` AUTO_INCREMENT = {$nextId};\n";
echo "ALTER TABLE `barber_services` AUTO_INCREMENT = 11;\n";
echo "ALTER TABLE `appointments` AUTO_INCREMENT = {$aid};\n";

echo "\nSET FOREIGN_KEY_CHECKS=1;\n";

$sql = ob_get_clean();
file_put_contents($outFile, $sql);
echo "Wrote {$outFile} (".strlen($sql)." bytes)\n";
