<?php

/**
 * Migration 0033 — Class schedule system (Studio / Fitness vertical)
 *
 * Three tables on top of the existing services + billing:
 *   - class_schedules:   recurring weekly rules ("every Mon 6pm Vinyasa, cap 20")
 *   - class_instances:   dated occurrences materialised from the rules (rolling window)
 *   - class_enrollments: a member's spot in an instance + attendance / check-in
 *
 * A class "type" reuses inst_instinctor_services (name/duration/capacity/price/
 * color/deposit), so these tables only model the schedule, occurrences and roster.
 */

if ( ! defined( 'ABSPATH' ) ) exit;

return [
    'description' => 'Class schedule system: schedules, instances, enrollments',

    'up' => [
        "CREATE TABLE IF NOT EXISTS {prefix}instinctor_class_schedules (
            id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            service_id bigint(20) unsigned NOT NULL,
            instructor varchar(150) DEFAULT NULL,
            day_of_week tinyint(1) NOT NULL DEFAULT 1,
            start_time time NOT NULL DEFAULT '09:00:00',
            duration int NOT NULL DEFAULT 60,
            capacity int NOT NULL DEFAULT 12,
            location varchar(150) DEFAULT NULL,
            active tinyint(1) NOT NULL DEFAULT 1,
            start_date date DEFAULT NULL,
            end_date date DEFAULT NULL,
            meta longtext,
            created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            KEY service_id (service_id),
            KEY day_of_week (day_of_week),
            KEY active (active)
        ) {charset}",

        "CREATE TABLE IF NOT EXISTS {prefix}instinctor_class_instances (
            id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            schedule_id bigint(20) unsigned DEFAULT NULL,
            service_id bigint(20) unsigned NOT NULL,
            instructor varchar(150) DEFAULT NULL,
            start_time datetime NOT NULL,
            end_time datetime NOT NULL,
            capacity int NOT NULL DEFAULT 12,
            booked_count int NOT NULL DEFAULT 0,
            status varchar(20) NOT NULL DEFAULT 'scheduled',
            meta longtext,
            created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            UNIQUE KEY sched_start (schedule_id, start_time),
            KEY service_id (service_id),
            KEY start_time (start_time),
            KEY status (status)
        ) {charset}",

        "CREATE TABLE IF NOT EXISTS {prefix}instinctor_class_enrollments (
            id bigint(20) unsigned NOT NULL AUTO_INCREMENT,
            instance_id bigint(20) unsigned NOT NULL,
            customer_id bigint(20) unsigned NOT NULL,
            status varchar(20) NOT NULL DEFAULT 'booked',
            source varchar(20) NOT NULL DEFAULT 'membership',
            order_id bigint(20) unsigned DEFAULT NULL,
            checked_in_at datetime DEFAULT NULL,
            meta longtext,
            created_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
            updated_at datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
            PRIMARY KEY (id),
            UNIQUE KEY instance_customer (instance_id, customer_id),
            KEY customer_id (customer_id),
            KEY status (status)
        ) {charset}",
    ],

    'down' => [
        "DROP TABLE IF EXISTS {prefix}instinctor_class_enrollments",
        "DROP TABLE IF EXISTS {prefix}instinctor_class_instances",
        "DROP TABLE IF EXISTS {prefix}instinctor_class_schedules",
    ],
];
