-- liquibase formatted sql
-- changeset 2fi:20260430_03_truck_lane_version_snapshot
-- preconditions onFail:MARK_RAN
-- precondition-sql-check expectedResult:0 SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'truck_lane_version'

CREATE TABLE IF NOT EXISTS `truck_lane_version`
(
    `id`            BIGINT       NOT NULL AUTO_INCREMENT,
    `created`       DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `createdBy`     VARCHAR(30)  NULL     DEFAULT NULL,
    `version`       INT          NOT NULL DEFAULT '0',
    `modified`      DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `modifiedBy`    VARCHAR(30)  NULL     DEFAULT NULL,
    `deleted`       TINYINT(1)   NOT NULL DEFAULT '0',
    `storeId`       VARCHAR(10)  NOT NULL,
    `truckLanceCode` VARCHAR(100) NOT NULL,
    `note`          VARCHAR(500) NULL     DEFAULT NULL,
    CONSTRAINT pk_truck_lane_version PRIMARY KEY (`id`)
);

-- When upgrading an existing database, CREATE TABLE IF NOT EXISTS will not add missing columns.
-- Old DB snapshots might already have `truck_lane_version` without `storeId`, which would break the index creation below.
SET @col_tlv_storeId := (
    SELECT COUNT(*)
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
      AND table_name = 'truck_lane_version'
      AND column_name = 'storeId'
);
SET @sql_add_tlv_storeId := IF(
    @col_tlv_storeId = 0,
    'ALTER TABLE `truck_lane_version` ADD COLUMN `storeId` VARCHAR(10) NULL DEFAULT NULL AFTER `deleted`',
    'SELECT 1'
);
PREPARE stmt_add_tlv_storeId FROM @sql_add_tlv_storeId;
EXECUTE stmt_add_tlv_storeId;
DEALLOCATE PREPARE stmt_add_tlv_storeId;

SET @idx_tlv := (
    SELECT COUNT(*)
    FROM information_schema.statistics
    WHERE table_schema = DATABASE()
      AND table_name = 'truck_lane_version'
      AND index_name = 'idx_tlv_lane'
);
SET @col_tlv_truckLanceCode := (
    SELECT COUNT(*)
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
      AND table_name = 'truck_lane_version'
      AND column_name = 'truckLanceCode'
);
SET @sql_tlv := IF(
    @idx_tlv = 0 AND @col_tlv_storeId > 0 AND @col_tlv_truckLanceCode > 0,
    'CREATE INDEX idx_tlv_lane ON `truck_lane_version` (`storeId`, `truckLanceCode`, `created`)',
    'SELECT 1'
);
PREPARE stmt_tlv FROM @sql_tlv;
EXECUTE stmt_tlv;
DEALLOCATE PREPARE stmt_tlv;

CREATE TABLE IF NOT EXISTS `truck_lane_version_line`
(
    `id`               BIGINT       NOT NULL AUTO_INCREMENT,
    `created`          DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `createdBy`        VARCHAR(30)  NULL     DEFAULT NULL,
    `version`          INT          NOT NULL DEFAULT '0',
    `modified`         DATETIME     NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `modifiedBy`       VARCHAR(30)  NULL     DEFAULT NULL,
    `deleted`          TINYINT(1)   NOT NULL DEFAULT '0',
    `truckLaneVersionId` BIGINT     NOT NULL,
    `truckRowId`       BIGINT       NOT NULL,
    `shopCode`         VARCHAR(50)  NULL     DEFAULT NULL,
    `branchName`       VARCHAR(255) NULL     DEFAULT NULL,
    `districtReference` VARCHAR(255) NULL    DEFAULT NULL,
    `loadingSequence`  INT          NULL     DEFAULT NULL,
    `departureTime`    VARCHAR(30)  NULL     DEFAULT NULL,
    `storeId`          VARCHAR(10)  NOT NULL,
    `remark`           VARCHAR(255) NULL     DEFAULT NULL,
    CONSTRAINT pk_truck_lane_version_line PRIMARY KEY (`id`),
    CONSTRAINT fk_tlvl_version FOREIGN KEY (`truckLaneVersionId`) REFERENCES `truck_lane_version` (`id`)
);

SET @col_tlvl_storeId := (
    SELECT COUNT(*)
    FROM information_schema.columns
    WHERE table_schema = DATABASE()
      AND table_name = 'truck_lane_version_line'
      AND column_name = 'storeId'
);
SET @sql_add_tlvl_storeId := IF(
    @col_tlvl_storeId = 0,
    'ALTER TABLE `truck_lane_version_line` ADD COLUMN `storeId` VARCHAR(10) NULL DEFAULT NULL AFTER `departureTime`',
    'SELECT 1'
);
PREPARE stmt_add_tlvl_storeId FROM @sql_add_tlvl_storeId;
EXECUTE stmt_add_tlvl_storeId;
DEALLOCATE PREPARE stmt_add_tlvl_storeId;

SET @idx_tlvl_v := (
    SELECT COUNT(*)
    FROM information_schema.statistics
    WHERE table_schema = DATABASE()
      AND table_name = 'truck_lane_version_line'
      AND index_name = 'idx_tlvl_version'
);
SET @sql_tlvl_v := IF(
    @idx_tlvl_v = 0,
    'CREATE INDEX idx_tlvl_version ON `truck_lane_version_line` (`truckLaneVersionId`)',
    'SELECT 1'
);
PREPARE stmt_tlvl_v FROM @sql_tlvl_v;
EXECUTE stmt_tlvl_v;
DEALLOCATE PREPARE stmt_tlvl_v;

SET @idx_tlvl_tr := (
    SELECT COUNT(*)
    FROM information_schema.statistics
    WHERE table_schema = DATABASE()
      AND table_name = 'truck_lane_version_line'
      AND index_name = 'idx_tlvl_truck_row'
);
SET @sql_tlvl_tr := IF(
    @idx_tlvl_tr = 0,
    'CREATE INDEX idx_tlvl_truck_row ON `truck_lane_version_line` (`truckRowId`)',
    'SELECT 1'
);
PREPARE stmt_tlvl_tr FROM @sql_tlvl_tr;
EXECUTE stmt_tlvl_tr;
DEALLOCATE PREPARE stmt_tlvl_tr;
