/*
 TechStackBase – Plan ↔ Module Mapping
 Version: v1.0.0
 Purpose: Define which modules are included in which subscription plans

 DESIGN PRINCIPLES
 -----------------
 • Server-authoritative entitlement mapping
 • Simple many-to-many relationship
 • Immutable in normal operation (edit via migrations)
 • High-read, low-write
 • Subscription & permission aware
*/

/* ============================================================
   PLAN MODULES TABLE
   ============================================================ */

CREATE TABLE plan_modules (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,

    /* Subscription plan */
    plan_code VARCHAR(64) NOT NULL,

    /* Module entitlement */
    module_code VARCHAR(64) NOT NULL,

    /* Metadata */
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

    /* Constraints */
    UNIQUE KEY uq_plan_module (plan_code, module_code),
    INDEX idx_plan_modules_plan (plan_code),
    INDEX idx_plan_modules_module (module_code),

    /* Foreign keys (soft integrity) */
    CONSTRAINT fk_plan_modules_plan
        FOREIGN KEY (plan_code)
        REFERENCES subscription_plans(code)
        ON DELETE CASCADE,

    CONSTRAINT fk_plan_modules_module
        FOREIGN KEY (module_code)
        REFERENCES modules(code)
        ON DELETE CASCADE
) ENGINE=InnoDB
  ROW_FORMAT=DYNAMIC
  COMMENT='Subscription plan module entitlements';
