-- ==============================================================================
-- Wellness Record module schema.
--
--   wellness_packages / wellness_package_lab_tests / wellness_package_radiology_tests
--       named bundles of lab + radiology tests (with target group, frequency and
--       non-orderable "other assessments"), offered as buttons on the wellness form
--   patient_wellness_records
--       one wellness check per row (vitals, body measurement, lifestyle...) plus
--       the case its lab/radiology tests were ordered on (case_id, lab_ordered,
--       radiology_ordered), which is what holds the ABDM link until they finish
--
-- Generated from the live schema of the database the module was built on, wellness
-- tables ONLY. Run this instead of doctrine:schema:update --force, which would also
-- apply unrelated drift (blood_matchings index, ward_beds TEXT/LONGTEXT). Safe to
-- re-run: every statement is CREATE TABLE IF NOT EXISTS.
--
-- After running it, load the standard packages (Basic Health Check-Up, Executive,
-- Cardiac Risk, Diabetes Care) for every hospital:
--     php bin/console app:seed-wellness-packages --apply
--
-- Also needed for the module to be usable, and NOT schema: create a user with
-- role ROLE_WELLNESS ("Wellness Staff" in the Add User screen).
-- ==============================================================================

CREATE TABLE IF NOT EXISTS `wellness_packages` (
  `id` int NOT NULL AUTO_INCREMENT,
  `name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL,
  `description` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `is_active` tinyint NOT NULL,
  `deleted_at` datetime DEFAULT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  `created_by` int DEFAULT NULL,
  `updated_by` int DEFAULT NULL,
  `hospital_id` int DEFAULT NULL,
  `target_group` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `frequency` varchar(30) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `other_assessments` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_214B7E1DE12AB56` (`created_by`),
  KEY `IDX_214B7E116FE72E1` (`updated_by`),
  KEY `IDX_214B7E163DBB69` (`hospital_id`),
  CONSTRAINT `FK_214B7E116FE72E1` FOREIGN KEY (`updated_by`) REFERENCES `users` (`id`),
  CONSTRAINT `FK_214B7E163DBB69` FOREIGN KEY (`hospital_id`) REFERENCES `hospitals` (`id`),
  CONSTRAINT `FK_214B7E1DE12AB56` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `wellness_package_lab_tests` (
  `wellness_package_id` int NOT NULL,
  `lab_test_id` int NOT NULL,
  PRIMARY KEY (`wellness_package_id`,`lab_test_id`),
  KEY `IDX_FEA9A089C0E3E42` (`wellness_package_id`),
  KEY `IDX_FEA9A0872CE4932` (`lab_test_id`),
  CONSTRAINT `FK_FEA9A0872CE4932` FOREIGN KEY (`lab_test_id`) REFERENCES `lab_tests` (`id`) ON DELETE CASCADE,
  CONSTRAINT `FK_FEA9A089C0E3E42` FOREIGN KEY (`wellness_package_id`) REFERENCES `wellness_packages` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `wellness_package_radiology_tests` (
  `wellness_package_id` int NOT NULL,
  `radiology_test_id` int NOT NULL,
  PRIMARY KEY (`wellness_package_id`,`radiology_test_id`),
  KEY `IDX_C2B130DA9C0E3E42` (`wellness_package_id`),
  KEY `IDX_C2B130DAA37CFF20` (`radiology_test_id`),
  CONSTRAINT `FK_C2B130DA9C0E3E42` FOREIGN KEY (`wellness_package_id`) REFERENCES `wellness_packages` (`id`) ON DELETE CASCADE,
  CONSTRAINT `FK_C2B130DAA37CFF20` FOREIGN KEY (`radiology_test_id`) REFERENCES `radiology_tests` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
CREATE TABLE IF NOT EXISTS `patient_wellness_records` (
  `id` int NOT NULL AUTO_INCREMENT,
  `recorded_at` date NOT NULL,
  `temperature` double DEFAULT NULL,
  `pulse_rate` int DEFAULT NULL,
  `respiratory_rate` int DEFAULT NULL,
  `bp_systolic` int DEFAULT NULL,
  `bp_diastolic` int DEFAULT NULL,
  `oxygen_saturation` int DEFAULT NULL,
  `height` double DEFAULT NULL,
  `weight` double DEFAULT NULL,
  `bmi` double DEFAULT NULL,
  `waist_circumference` double DEFAULT NULL,
  `hip_circumference` double DEFAULT NULL,
  `physical_activity_note` longtext COLLATE utf8mb4_unicode_ci,
  `last_menstrual_period` date DEFAULT NULL,
  `womens_health_note` longtext COLLATE utf8mb4_unicode_ci,
  `tobacco_use` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `alcohol_use` varchar(20) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `diet_note` longtext COLLATE utf8mb4_unicode_ci,
  `general_assessment_note` longtext COLLATE utf8mb4_unicode_ci,
  `deleted_at` datetime DEFAULT NULL,
  `created_at` datetime NOT NULL,
  `updated_at` datetime NOT NULL,
  `patient_id` int NOT NULL,
  `created_by` int DEFAULT NULL,
  `updated_by` int DEFAULT NULL,
  `hospital_id` int DEFAULT NULL,
  `lab_ordered` tinyint NOT NULL DEFAULT '0',
  `radiology_ordered` tinyint NOT NULL DEFAULT '0',
  `case_id` int DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `IDX_9B500B806B899279` (`patient_id`),
  KEY `IDX_9B500B80DE12AB56` (`created_by`),
  KEY `IDX_9B500B8016FE72E1` (`updated_by`),
  KEY `idx_wellness_record_patient_recorded` (`patient_id`,`recorded_at`),
  KEY `idx_wellness_record_hospital` (`hospital_id`),
  KEY `IDX_9B500B80CF10D4F5` (`case_id`),
  CONSTRAINT `FK_9B500B8016FE72E1` FOREIGN KEY (`updated_by`) REFERENCES `users` (`id`),
  CONSTRAINT `FK_9B500B8063DBB69` FOREIGN KEY (`hospital_id`) REFERENCES `hospitals` (`id`),
  CONSTRAINT `FK_9B500B806B899279` FOREIGN KEY (`patient_id`) REFERENCES `patients` (`id`),
  CONSTRAINT `FK_9B500B80CF10D4F5` FOREIGN KEY (`case_id`) REFERENCES `cases` (`id`),
  CONSTRAINT `FK_9B500B80DE12AB56` FOREIGN KEY (`created_by`) REFERENCES `users` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
