-- =====================================================================
-- WiFi Zone : Lot 16 (tickets générés par la plateforme, modèles d'impression)
-- À importer UNE SEULE FOIS dans phpMyAdmin (onglet « Importer »), après le Lot 15.
-- Équivaut à : php artisan migrate (migration 2026_10_05_000001).
-- =====================================================================

CREATE TABLE `ticket_batches` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `router_id` BIGINT UNSIGNED NOT NULL,
  `reseller_id` BIGINT UNSIGNED NULL,
  `allocation_id` BIGINT UNSIGNED NULL,
  `profile` VARCHAR(120) NOT NULL,
  `prefix` VARCHAR(8) NOT NULL,
  `code_length` TINYINT UNSIGNED NOT NULL DEFAULT 10,
  `charset` VARCHAR(10) NOT NULL DEFAULT 'digits',
  `mode` VARCHAR(10) NOT NULL DEFAULT 'voucher',
  `limit_uptime` VARCHAR(20) NULL,
  `comment` VARCHAR(64) NOT NULL,
  `vc_number` SMALLINT UNSIGNED NOT NULL,
  `quantity` INT UNSIGNED NOT NULL,
  `created_count` INT UNSIGNED NOT NULL DEFAULT 0,
  `failed_count` INT UNSIGNED NOT NULL DEFAULT 0,
  `record_allocation` TINYINT(1) NOT NULL DEFAULT 1,
  `status` VARCHAR(12) NOT NULL DEFAULT 'running',
  `last_error` TEXT NULL,
  `created_by` BIGINT UNSIGNED NULL,
  `created_at` TIMESTAMP NULL,
  `updated_at` TIMESTAMP NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `ticket_batches_router_comment_unique` (`router_id`, `comment`),
  CONSTRAINT `ticket_batches_router_id_foreign` FOREIGN KEY (`router_id`) REFERENCES `routers` (`id`),
  CONSTRAINT `ticket_batches_reseller_id_foreign` FOREIGN KEY (`reseller_id`) REFERENCES `resellers` (`id`) ON DELETE SET NULL,
  CONSTRAINT `ticket_batches_allocation_id_foreign` FOREIGN KEY (`allocation_id`) REFERENCES `allocations` (`id`) ON DELETE SET NULL,
  CONSTRAINT `ticket_batches_created_by_foreign` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE `print_templates` (
  `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name` VARCHAR(80) NOT NULL,
  `columns` TINYINT UNSIGNED NOT NULL DEFAULT 4,
  `body` TEXT NOT NULL,
  `css` TEXT NULL,
  `is_default` TINYINT(1) NOT NULL DEFAULT 0,
  `created_at` TIMESTAMP NULL,
  `updated_at` TIMESTAMP NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `print_templates` (`name`, `columns`, `body`, `css`, `is_default`, `created_at`, `updated_at`) VALUES
('Compact (5 par ligne)', 5, '<div class="t"><div class="h">{entreprise}</div><div class="c">{code}</div>{si_motdepasse}<div class="p">Mot de passe : <b>{motdepasse}</b></div>{fin}<div class="f"><span>{prix}</span><span>{validite}</span></div><div class="n">{numero} · {vendeur}</div></div>', '.t{border:1px dashed #94a3b8;border-radius:6px;padding:6px 8px;font-family:Arial,sans-serif;text-align:center}.h{font-size:9px;color:#475569;text-transform:uppercase;letter-spacing:.05em}.c{font-size:17px;font-weight:700;letter-spacing:.08em;margin:4px 0;color:#0a2a66}.p{font-size:10px}.f{display:flex;justify-content:space-between;font-size:10px;font-weight:700;border-top:1px solid #e2e8f0;padding-top:3px}.n{font-size:8px;color:#94a3b8;margin-top:2px}', 1, UTC_TIMESTAMP(), UTC_TIMESTAMP()),
('Carte avec QR code (3 par ligne)', 3, '<div class="card"><div class="top">{logo}<div><div class="brand">{entreprise}</div><div class="offer">{validite} · {prix}</div></div></div><div class="mid"><div class="qr">{qr}</div><div><div class="lbl">Code WiFi</div><div class="code">{code}</div>{si_motdepasse}<div class="lbl">Mot de passe</div><div class="code small">{motdepasse}</div>{fin}<div class="hint">Connectez-vous au WiFi, ouvrez {dns} ou scannez le QR code.</div></div></div><div class="foot">N° {numero} · {vendeur}</div></div>', '.card{border:1.5px solid #0a2a66;border-radius:10px;padding:8px;font-family:Arial,sans-serif;color:#0f172a}.top{display:flex;align-items:center;gap:6px;border-bottom:1px solid #e2e8f0;padding-bottom:5px}.top img{height:26px;width:26px;object-fit:contain}.brand{font-weight:700;font-size:11px;color:#0a2a66}.offer{font-size:10px;color:#c2410c;font-weight:700}.mid{display:flex;gap:8px;align-items:center;padding-top:6px}.mid>div:last-child{min-width:0}.qr{flex-shrink:0}.qr svg{width:72px;height:72px;display:block}.lbl{font-size:8px;color:#64748b;text-transform:uppercase}.code{font-size:16px;font-weight:700;letter-spacing:.06em;word-break:break-all}.code.small{font-size:13px}.hint{font-size:8px;color:#64748b;margin-top:3px}.foot{font-size:8px;color:#94a3b8;text-align:right;margin-top:4px}', 0, UTC_TIMESTAMP(), UTC_TIMESTAMP());

INSERT INTO `migrations` (`migration`, `batch`)
SELECT '2026_10_05_000001_create_ticket_batches_and_print_templates', COALESCE(MAX(`batch`), 0) + 1 FROM `migrations`;
