-- CreateTable
CREATE TABLE `account_user` (
    `id` VARCHAR(36) NOT NULL,
    `account` VARCHAR(50) NOT NULL,
    `password` VARCHAR(255) NOT NULL,
    `email` VARCHAR(100) NOT NULL,
    `role` ENUM('ADMIN') NOT NULL DEFAULT 'ADMIN',
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `account_user_account_key`(`account`),
    UNIQUE INDEX `account_user_email_key`(`email`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `delivery_order` (
    `id` VARCHAR(36) NOT NULL,
    `orderNo` VARCHAR(40) NOT NULL,
    `governorateName` VARCHAR(100) NOT NULL,
    `areaName` VARCHAR(100) NOT NULL,
    `nearestLandmark` VARCHAR(255) NOT NULL,
    `customerName` VARCHAR(100) NOT NULL,
    `customerPhone` VARCHAR(30) NOT NULL,
    `driverName` VARCHAR(100) NOT NULL,
    `orderType` VARCHAR(100) NOT NULL,
    `status` ENUM('PENDING', 'IN_PROGRESS', 'DELIVERED', 'CANCELLED') NOT NULL DEFAULT 'PENDING',
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `delivery_order_orderNo_key`(`orderNo`),
    INDEX `delivery_order_status_idx`(`status`),
    INDEX `delivery_order_governorateName_idx`(`governorateName`),
    INDEX `delivery_order_driverName_idx`(`driverName`),
    INDEX `delivery_order_createdAt_idx`(`createdAt`),
    INDEX `delivery_order_customerPhone_idx`(`customerPhone`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `delivery_order_status_history` (
    `id` VARCHAR(36) NOT NULL,
    `deliveryOrderId` VARCHAR(36) NOT NULL,
    `fromStatus` ENUM('PENDING', 'IN_PROGRESS', 'DELIVERED', 'CANCELLED') NULL,
    `toStatus` ENUM('PENDING', 'IN_PROGRESS', 'DELIVERED', 'CANCELLED') NOT NULL,
    `changedById` VARCHAR(36) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    INDEX `delivery_order_status_history_deliveryOrderId_createdAt_idx`(`deliveryOrderId`, `createdAt`),
    INDEX `delivery_order_status_history_toStatus_idx`(`toStatus`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `delivery_order_status_history` ADD CONSTRAINT `delivery_order_status_history_deliveryOrderId_fkey` FOREIGN KEY (`deliveryOrderId`) REFERENCES `delivery_order`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `delivery_order_status_history` ADD CONSTRAINT `delivery_order_status_history_changedById_fkey` FOREIGN KEY (`changedById`) REFERENCES `account_user`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;
