-- CreateTable
CREATE TABLE `Users` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(255) NOT NULL,
    `username` VARCHAR(191) NOT NULL,
    `phone` VARCHAR(191) NULL,
    `password` VARCHAR(255) NOT NULL,
    `area` VARCHAR(191) NULL,
    `isAdmin` BOOLEAN NOT NULL DEFAULT false,
    `isActive` BOOLEAN NOT NULL DEFAULT true,
    `isDeleted` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `Users_username_key`(`username`),
    UNIQUE INDEX `Users_phone_key`(`phone`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `Shops` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `area` VARCHAR(191) NOT NULL,
    `owner` VARCHAR(191) NOT NULL,
    `phone` VARCHAR(191) NOT NULL,
    `NTN` VARCHAR(191) NULL,
    `STRN` VARCHAR(191) NULL,
    `paymentTermsInDays` INTEGER NULL DEFAULT 0,
    `tradeType` ENUM('GENERAL_TRADE', 'MART_TRADE') NULL DEFAULT 'GENERAL_TRADE',
    `isActive` BOOLEAN NOT NULL DEFAULT true,
    `isDeleted` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `Shops_NTN_key`(`NTN`),
    UNIQUE INDEX `Shops_STRN_key`(`STRN`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `Products` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `productCode` VARCHAR(191) NULL,
    `generalPrice` DOUBLE NOT NULL,
    `martPrice` DOUBLE NOT NULL,
    `stockQuantity` INTEGER NOT NULL,
    `minimumStockQuantity` INTEGER NULL DEFAULT 0,
    `isActive` BOOLEAN NOT NULL DEFAULT true,
    `isDeleted` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `Products_productCode_key`(`productCode`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `Orders` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `shopId` INTEGER NOT NULL,
    `bookerId` INTEGER NOT NULL,
    `shippingDate` DATETIME(3) NULL,
    `status` ENUM('PENDING', 'SHIPPED', 'PAID', 'CANCELLED') NOT NULL DEFAULT 'PENDING',
    `lineItems` JSON NOT NULL,
    `gstEnabled` BOOLEAN NOT NULL DEFAULT false,
    `gstPercentage` INTEGER NOT NULL DEFAULT 18,
    `gstAmount` INTEGER NOT NULL DEFAULT 0,
    `discountPercentage` INTEGER NOT NULL DEFAULT 0,
    `discountAmount` INTEGER NOT NULL DEFAULT 0,
    `subtotalAmount` DOUBLE NOT NULL,
    `totalAmount` DOUBLE NOT NULL,
    `paymentMethod` ENUM('CASH', 'CHEQUE', 'ONLINE_TRANSFER', 'CREDIT') NOT NULL DEFAULT 'CASH',
    `remainingDaysToPay` INTEGER NULL DEFAULT 0,
    `dueDate` DATETIME(3) NULL,
    `isActive` BOOLEAN NOT NULL DEFAULT true,
    `isDeleted` BOOLEAN NOT NULL DEFAULT false,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `Orders` ADD CONSTRAINT `Orders_shopId_fkey` FOREIGN KEY (`shopId`) REFERENCES `Shops`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Orders` ADD CONSTRAINT `Orders_bookerId_fkey` FOREIGN KEY (`bookerId`) REFERENCES `Users`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
