-- ============================================================
-- TumbuhSehat v3 - Multi-entity (multi clinic/RS) + versioning
-- Run this AFTER schema.sql and schema_v2.sql
-- ============================================================

USE `tumbuhsehat`;

SET FOREIGN_KEY_CHECKS = 0;

-- ---------- clinics (RS / klinik / praktik) ----------
CREATE TABLE IF NOT EXISTS `clinics` (
  `id`         INT(11) NOT NULL AUTO_INCREMENT,
  `name`       VARCHAR(160) NOT NULL,
  `slug`       VARCHAR(120) NOT NULL,
  `address`    TEXT DEFAULT NULL,
  `phone`      VARCHAR(32) DEFAULT NULL,
  `logo_url`   VARCHAR(255) DEFAULT NULL,
  `is_active`  TINYINT(1) NOT NULL DEFAULT 1,
  `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 `uq_clinic_slug` (`slug`),
  KEY `idx_clinic_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- ---------- users: add clinic scope + new roles ----------
-- 'admin' is kept in the enum for legacy rows; v3 renames it to 'clinic_admin'.
ALTER TABLE `users`
  MODIFY COLUMN `role` ENUM('parent','therapist','doctor','admin','clinic_admin','super_admin') NOT NULL;

ALTER TABLE `users`
  ADD COLUMN `clinic_id` INT(11) DEFAULT NULL COMMENT 'Owning clinic; NULL for super_admin' AFTER `role`,
  ADD KEY `idx_user_clinic` (`clinic_id`),
  ADD CONSTRAINT `fk_user_clinic` FOREIGN KEY (`clinic_id`) REFERENCES `clinics`(`id`) ON DELETE SET NULL;

-- legacy admin -> clinic_admin
UPDATE `users` SET `role` = 'clinic_admin' WHERE `role` = 'admin';

-- ---------- children: add clinic scope ----------
ALTER TABLE `children`
  ADD COLUMN `clinic_id` INT(11) DEFAULT NULL COMMENT 'Owning clinic' AFTER `parent_id`,
  ADD KEY `idx_child_clinic` (`clinic_id`),
  ADD CONSTRAINT `fk_child_clinic` FOREIGN KEY (`clinic_id`) REFERENCES `clinics`(`id`) ON DELETE SET NULL;

-- ---------- app versions (release management) ----------
CREATE TABLE IF NOT EXISTS `app_versions` (
  `id`            INT(11) NOT NULL AUTO_INCREMENT,
  `version`       VARCHAR(32) NOT NULL,
  `semver_major`  INT(4) NOT NULL DEFAULT 0,
  `semver_minor`  INT(4) NOT NULL DEFAULT 0,
  `semver_patch`  INT(4) NOT NULL DEFAULT 0,
  `release_notes` TEXT DEFAULT NULL,
  `changelog`     JSON DEFAULT NULL COMMENT 'Array of change entries',
  `built_at`      DATETIME DEFAULT NULL,
  `released_at`   DATETIME DEFAULT NULL,
  `status`        ENUM('draft','released','deprecated','retired') NOT NULL DEFAULT 'draft',
  `git_commit`    VARCHAR(64) DEFAULT NULL,
  `git_branch`    VARCHAR(120) DEFAULT NULL,
  `created_by`    INT(11) DEFAULT NULL,
  `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 `uq_app_version` (`version`),
  KEY `idx_version_status` (`status`),
  KEY `idx_version_created_by` (`created_by`),
  CONSTRAINT `fk_version_creator` FOREIGN KEY (`created_by`) REFERENCES `users`(`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- ---------- clinic scoped helper views (optional conveniences) ----------
-- NOTE: intentionally not created; scoping is enforced in the API layer.

SET FOREIGN_KEY_CHECKS = 1;
