-- CreateTable
CREATE TABLE `logs` (
    `id` CHAR(25) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `userId` CHAR(25) NOT NULL,
    `ip` VARCHAR(191) NOT NULL,
    `log` TEXT NOT NULL,
    `action` VARCHAR(191) NOT NULL DEFAULT 'view',
    `table` VARCHAR(191) NOT NULL,

    INDEX `logs_userId_idx`(`userId`),
    INDEX `logs_action_idx`(`action`),
    INDEX `logs_table_idx`(`table`),
    INDEX `logs_createdAt_idx`(`createdAt`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `users` (
    `id` CHAR(25) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `deletedAt` DATETIME(3) NULL,
    `email` VARCHAR(191) NOT NULL,
    `emailVerify` DATETIME(3) NULL,
    `password` VARCHAR(191) NOT NULL,
    `name` VARCHAR(191) NOT NULL,
    `phone` VARCHAR(191) NULL,
    `type` VARCHAR(191) NULL DEFAULT 'admin',

    UNIQUE INDEX `users_email_key`(`email`),
    INDEX `users_email_idx`(`email`),
    INDEX `users_name_idx`(`name`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `companies` (
    `id` CHAR(25) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `deletedAt` DATETIME(3) NULL,
    `fantasy` VARCHAR(191) NOT NULL,
    `socialReason` VARCHAR(191) NOT NULL,
    `cnpj` VARCHAR(191) NOT NULL,
    `ie` VARCHAR(191) NOT NULL,
    `phone` VARCHAR(191) NOT NULL,
    `cel` VARCHAR(191) NULL,
    `email` VARCHAR(191) NULL,
    `address` VARCHAR(191) NULL,
    `codePostal` VARCHAR(191) NULL,
    `city` VARCHAR(191) NULL,
    `state` VARCHAR(191) NULL,
    `imageLogo` VARCHAR(191) NULL,

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

-- CreateTable
CREATE TABLE `customers_settings` (
    `id` CHAR(25) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `deletedAt` DATETIME(3) NULL,
    `companyId` CHAR(25) NULL,
    `field` VARCHAR(191) NOT NULL,
    `fieldValues` VARCHAR(191) NOT NULL,
    `size` INTEGER NOT NULL DEFAULT 0,
    `required` VARCHAR(191) NOT NULL DEFAULT 'no',
    `type` VARCHAR(191) NOT NULL DEFAULT 'text',
    `position` INTEGER NOT NULL DEFAULT 0,
    `published` VARCHAR(191) NOT NULL DEFAULT 'yes',
    `filter` VARCHAR(191) NOT NULL DEFAULT 'no',

    INDEX `customers_settings_published_idx`(`published`),
    INDEX `customers_settings_filter_idx`(`filter`),
    INDEX `customers_settings_position_idx`(`position`),
    INDEX `customers_settings_companyId_idx`(`companyId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `customers` (
    `id` CHAR(25) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `deletedAt` DATETIME(3) NULL,
    `data` TEXT NULL,

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

-- CreateTable
CREATE TABLE `orders_settings` (
    `id` CHAR(25) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `deletedAt` DATETIME(3) NULL,
    `companyId` CHAR(25) NULL,
    `field` VARCHAR(191) NOT NULL,
    `fieldValues` VARCHAR(191) NOT NULL,
    `size` INTEGER NOT NULL DEFAULT 0,
    `required` VARCHAR(191) NOT NULL DEFAULT 'no',
    `type` VARCHAR(191) NOT NULL DEFAULT 'text',
    `position` INTEGER NOT NULL DEFAULT 0,
    `published` VARCHAR(191) NOT NULL DEFAULT 'yes',
    `filter` VARCHAR(191) NOT NULL DEFAULT 'no',

    INDEX `orders_settings_published_idx`(`published`),
    INDEX `orders_settings_filter_idx`(`filter`),
    INDEX `orders_settings_position_idx`(`position`),
    INDEX `orders_settings_companyId_idx`(`companyId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `orders` (
    `id` CHAR(25) NOT NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,
    `deletedAt` DATETIME(3) NULL,
    `userId` CHAR(25) NULL,
    `companyId` CHAR(25) NULL,
    `data` TEXT NULL,
    `totalParts` DECIMAL NOT NULL DEFAULT 0.00,
    `totalService` DECIMAL NOT NULL DEFAULT 0.00,
    `totalDisplacement` DECIMAL NOT NULL DEFAULT 0.00,
    `totalDiscont` DECIMAL NOT NULL DEFAULT 0.00,
    `totalAmount` DECIMAL NOT NULL DEFAULT 0.00,

    INDEX `orders_userId_idx`(`userId`),
    INDEX `orders_companyId_idx`(`companyId`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `logs` ADD CONSTRAINT `logs_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `customers_settings` ADD CONSTRAINT `customers_settings_companyId_fkey` FOREIGN KEY (`companyId`) REFERENCES `companies`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `orders_settings` ADD CONSTRAINT `orders_settings_companyId_fkey` FOREIGN KEY (`companyId`) REFERENCES `companies`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `orders` ADD CONSTRAINT `orders_userId_fkey` FOREIGN KEY (`userId`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `orders` ADD CONSTRAINT `orders_companyId_fkey` FOREIGN KEY (`companyId`) REFERENCES `companies`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
