-- Migrasi Penambahan Fitur Kategori (Grouping)

CREATE TABLE `kategori_turnamen` (
  `id_kategori` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  `id_turnamen` INT(11) UNSIGNED NOT NULL,
  `nama_kategori` VARCHAR(100) NOT NULL,
  `slug_kategori` VARCHAR(100) NOT NULL,
  `maksimal_peserta` INT(11) DEFAULT 32,
  `status_kategori` ENUM('pendaftaran', 'berjalan', 'selesai') DEFAULT 'pendaftaran',
  `created_at` DATETIME DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id_kategori`),
  FOREIGN KEY (`id_turnamen`) REFERENCES `turnamen`(`id_turnamen`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Modifikasi tabel peserta
ALTER TABLE `peserta` ADD COLUMN `id_kategori` INT(11) UNSIGNED NULL AFTER `id_turnamen`;
ALTER TABLE `peserta` ADD CONSTRAINT `fk_peserta_kategori` FOREIGN KEY (`id_kategori`) REFERENCES `kategori_turnamen`(`id_kategori`) ON DELETE SET NULL;

-- Modifikasi tabel pertandingan
ALTER TABLE `pertandingan` ADD COLUMN `id_kategori` INT(11) UNSIGNED NULL AFTER `id_turnamen`;
ALTER TABLE `pertandingan` ADD CONSTRAINT `fk_pertandingan_kategori` FOREIGN KEY (`id_kategori`) REFERENCES `kategori_turnamen`(`id_kategori`) ON DELETE SET NULL;
