-- Migration 001: Initial schema for cpone_dashboard
-- Naming convention: table=snake_case, column=Prefix_PascalCase (mengikuti cpone)
-- Tidak menggunakan foreign key constraint

-- ============================================================
-- mcu_project
-- Source: cpone.mgm_mcu JOIN cpone.corporate
-- ============================================================
CREATE TABLE IF NOT EXISTS mcu_project (
    Mcu_ProjectID               INT AUTO_INCREMENT PRIMARY KEY,
    Mcu_ProjectMcuID            INT NOT NULL,           -- Mgm_McuID
    Mcu_ProjectCorporateID      INT NOT NULL,           -- Mgm_McuCorporateID
    Mcu_ProjectCorporateName    VARCHAR(255),           -- corporate.CorporateName
    Mcu_ProjectNumber           VARCHAR(50),            -- Mgm_McuNumber
    Mcu_ProjectLabel            VARCHAR(255),           -- Mgm_McuLabel
    Mcu_ProjectBranchID         INT DEFAULT 0,          -- Mgm_McuM_BranchID
    Mcu_ProjectStartDate        DATE,                   -- Mgm_McuStartDate
    Mcu_ProjectEndDate          DATE,                   -- Mgm_McuEndDate
    Mcu_ProjectIsActive         CHAR(1) DEFAULT 'Y',    -- Mgm_McuIsActive
    Mcu_ProjectTotalParticipant INT DEFAULT 0,          -- Mgm_McuTotalParticipant
    Mcu_ProjectSyncedAt         DATETIME,
    UNIQUE KEY uq_mcu_id (Mcu_ProjectMcuID),
    INDEX idx_is_active (Mcu_ProjectIsActive),
    INDEX idx_dates (Mcu_ProjectStartDate, Mcu_ProjectEndDate)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


-- ============================================================
-- mcu_patient
-- Source: cpone.mcu_preregister_patients
-- ============================================================
CREATE TABLE IF NOT EXISTS mcu_patient (
    Mcu_PatientID               INT AUTO_INCREMENT PRIMARY KEY,
    Mcu_PatientPreregisterID    INT NOT NULL,           -- Mcu_PreregisterPatientsID
    Mcu_PatientMcuID            INT NOT NULL,           -- Mcu_PreregisterPatientsMgm_McuID
    Mcu_PatientName             VARCHAR(150),           -- Mcu_PreregisterPatientsPatientName
    Mcu_PatientNIP              VARCHAR(50),            -- Mcu_PreregisterPatientsNIP
    Mcu_PatientGender           VARCHAR(10),            -- Mcu_PreregisterPatientsGender
    Mcu_PatientDOB              DATE,                   -- Mcu_PreregisterPatientsDOB
    Mcu_PatientDepartment       VARCHAR(500),           -- Mcu_PreregisterPatientsDepartment
    Mcu_PatientDivision         VARCHAR(500),           -- Mcu_PreregisterPatientsDivisi
    Mcu_PatientPosisi           VARCHAR(500),           -- Mcu_PreregisterPatientsPosisi
    Mcu_PatientIsRegistered     CHAR(1) DEFAULT 'N',    -- Mcu_PreregisterPatientsIsRegistered
    Mcu_PatientOrderID          INT DEFAULT 0,          -- Mcu_PreregisterPatientsT_OrderHeaderID
    Mcu_PatientIsActive         CHAR(1) DEFAULT 'Y',    -- Mcu_PreregisterPatientsIsActive
    Mcu_PatientSyncedAt         DATETIME,
    UNIQUE KEY uq_preregister_id (Mcu_PatientPreregisterID),
    INDEX idx_mcu_id (Mcu_PatientMcuID),
    INDEX idx_order_id (Mcu_PatientOrderID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


-- ============================================================
-- mcu_patient_schedule
-- Source: cpone.mcu_preregister_date
-- Catatan: tidak semua pasien punya jadwal (opsional)
-- ============================================================
CREATE TABLE IF NOT EXISTS mcu_patient_schedule (
    Mcu_PatientScheduleID               INT AUTO_INCREMENT PRIMARY KEY,
    Mcu_PatientSchedulePreregisterID    INT NOT NULL,           -- Mcu_PreregisterDateMcu_PreregisterPatientsID
    Mcu_PatientScheduleDate             DATE NOT NULL,          -- Mcu_PreregisterDateCheckinSchedule
    Mcu_PatientScheduleIsActive         CHAR(1) DEFAULT 'Y',    -- Mcu_PreregisterDateIsActive
    Mcu_PatientScheduleSyncedAt         DATETIME,
    INDEX idx_preregister_id (Mcu_PatientSchedulePreregisterID),
    INDEX idx_schedule_date (Mcu_PatientScheduleDate)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


-- ============================================================
-- mcu_checkinout
-- Source: cpone.preregister_checkin_checkout
-- Catatan: 1 pasien bisa punya > 1 baris (multi-hari)
-- ============================================================
CREATE TABLE IF NOT EXISTS mcu_checkinout (
    Mcu_CheckinoutID                INT AUTO_INCREMENT PRIMARY KEY,
    Mcu_CheckinoutCheckinoutID      INT NOT NULL,           -- PreregisterCheckInCheckOutID
    Mcu_CheckinoutPreregisterID     INT NOT NULL,           -- PreregisterCheckInCheckOutPreregisterID
    Mcu_CheckinoutOrderID           INT DEFAULT 0,          -- PreregisterCheckInCheckOutT_OrderHeaderID
    Mcu_CheckinoutDate              DATE NOT NULL,          -- PreregisterCheckInCheckOutDate
    Mcu_CheckinoutInTime            TIME,                   -- PreregisterCheckInCheckOutInTime
    Mcu_CheckinoutOutTime           TIME,                   -- PreregisterCheckInCheckOutOutTime
    Mcu_CheckinoutOutUserID         INT DEFAULT 0,          -- PreregisterCheckInCheckOutOutUserID
    Mcu_CheckinoutNextDate          DATE,                   -- PreregisterCheckInCheckOutNextPreregisterDate
    Mcu_CheckinoutNote              TEXT,                   -- PreregisterCheckInCheckOutNote
    Mcu_CheckinoutIsActive          CHAR(1) DEFAULT 'Y',    -- PreregisterCheckInCheckOutIsActive
    Mcu_CheckinoutSyncedAt          DATETIME,
    UNIQUE KEY uq_checkinout_segment (
        Mcu_CheckinoutPreregisterID,
        Mcu_CheckinoutDate,
        Mcu_CheckinoutInTime
    ),
    INDEX idx_preregister_id (Mcu_CheckinoutPreregisterID),
    INDEX idx_date (Mcu_CheckinoutDate),
    INDEX idx_order_id (Mcu_CheckinoutOrderID)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;


-- ============================================================
-- mcu_station_progress  (Phase 2)
-- Source: cpone.t_ordersample (lab)
--       + cpone.t_orderdetail->t_test->group_resultdetail->group_result (nonlab)
-- ETL flatten kedua sumber menjadi satu tabel
-- ============================================================
CREATE TABLE IF NOT EXISTS mcu_station_progress (
    Mcu_StationProgressID               INT AUTO_INCREMENT PRIMARY KEY,
    Mcu_StationProgressOrderID          INT NOT NULL,           -- T_OrderHeaderID
    Mcu_StationProgressPreregisterID    INT NOT NULL,
    Mcu_StationProgressMcuID            INT NOT NULL,
    Mcu_StationProgressStationID        INT NOT NULL,           -- T_SampleStationID
    Mcu_StationProgressStationName      VARCHAR(100),           -- T_SampleStationName
    Mcu_StationProgressSource           VARCHAR(10),            -- 'lab' | 'nonlab'
    Mcu_StationProgressCheckinDate      DATE,                   -- tanggal checkin pasien
    Mcu_StationProgressSamplingAt       DATETIME,               -- lab: SamplingDate+Time
    Mcu_StationProgressReceiveAt        DATETIME,               -- lab: ReceiveDate+Time
    Mcu_StationProgressProcessAt        DATETIME,               -- lab: ProcessingDate+Time
    Mcu_StationProgressDoneAt           DATETIME,               -- lab: DoneDate+Time | nonlab: result entry time
    Mcu_StationProgressSyncedAt         DATETIME,
    INDEX idx_order_id (Mcu_StationProgressOrderID),
    INDEX idx_preregister_id (Mcu_StationProgressPreregisterID),
    INDEX idx_mcu_id (Mcu_StationProgressMcuID),
    INDEX idx_station_id (Mcu_StationProgressStationID),
    INDEX idx_checkin_date (Mcu_StationProgressCheckinDate)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
