<?php
declare(strict_types=1);

return [
    'up' => "
        ALTER TABLE `sa_facilities`
            ADD COLUMN `pool_grid_rows` TINYINT UNSIGNED NULL AFTER `operating_hours_json`,
            ADD COLUMN `pool_grid_cols` TINYINT UNSIGNED NULL AFTER `pool_grid_rows`;

        CREATE TABLE `sa_pool_zone_bookings` (
            `id` BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
            `facility_id` BIGINT UNSIGNED NOT NULL,
            `booking_date` DATE NOT NULL,
            `start_time` TIME NOT NULL,
            `end_time` TIME NOT NULL,
            `zone_row` TINYINT UNSIGNED NOT NULL COMMENT '0-based row index',
            `zone_col` TINYINT UNSIGNED NOT NULL COMMENT '0-based col index',
            `assignment_type` VARCHAR(20) NOT NULL COMMENT 'training, blocked, maintenance, hourly',
            `group_id` BIGINT UNSIGNED NULL,
            `coach_id` BIGINT UNSIGNED NULL,
            `label` VARCHAR(200) NULL,
            `notes` TEXT NULL,
            `status` VARCHAR(20) NOT NULL DEFAULT 'active' COMMENT 'active, cancelled',
            `created_by` BIGINT UNSIGNED NULL,
            `cancelled_by` BIGINT UNSIGNED NULL,
            `cancelled_at` TIMESTAMP NULL,
            `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
            `updated_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            INDEX `idx_sa_pzb_lookup` (`facility_id`, `booking_date`, `start_time`, `end_time`),
            INDEX `idx_sa_pzb_zone` (`facility_id`, `zone_row`, `zone_col`),
            INDEX `idx_sa_pzb_group` (`group_id`),
            UNIQUE KEY `uq_sa_pzb_slot` (`facility_id`, `booking_date`, `start_time`, `zone_row`, `zone_col`),
            CONSTRAINT `fk_sa_pzb_facility` FOREIGN KEY (`facility_id`) REFERENCES `sa_facilities`(`id`) ON DELETE CASCADE,
            CONSTRAINT `fk_sa_pzb_group` FOREIGN KEY (`group_id`) REFERENCES `sa_groups`(`id`) ON DELETE SET NULL,
            CONSTRAINT `fk_sa_pzb_coach` FOREIGN KEY (`coach_id`) REFERENCES `sa_coaches`(`id`) ON DELETE SET NULL
        ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
    ",
    'down' => "
        DROP TABLE IF EXISTS `sa_pool_zone_bookings`;
        ALTER TABLE `sa_facilities` DROP COLUMN `pool_grid_rows`, DROP COLUMN `pool_grid_cols`
    ",
];
