-- CreateTable
CREATE TABLE `User` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `username` VARCHAR(191) NOT NULL,
    `email` VARCHAR(191) NOT NULL,
    `password` VARCHAR(191) NOT NULL,
    `token` VARCHAR(191) NULL,
    `refresh_token` VARCHAR(191) NULL,
    `verified` DATETIME(3) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `last_login` DATETIME(3) NULL,

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

-- CreateTable
CREATE TABLE `Anthill` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `owner_id` INTEGER NOT NULL,
    `position_x` INTEGER NOT NULL,
    `position_y` INTEGER NOT NULL,
    `eggs` INTEGER NOT NULL,
    `larva` INTEGER NOT NULL,
    `ants` INTEGER NOT NULL,
    `ants_busy` INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE `Construction` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `preview` INTEGER NULL,
    `effects` JSON NULL,

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

-- CreateTable
CREATE TABLE `Investigation` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `preview` INTEGER NULL,
    `effects` JSON NULL,

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

-- CreateTable
CREATE TABLE `Ant` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `type` ENUM('W', 'S') NOT NULL,
    `attack` INTEGER NOT NULL,
    `defense` INTEGER NOT NULL,
    `speed` INTEGER NOT NULL,
    `heal` INTEGER NOT NULL,
    `capacity` INTEGER NOT NULL,
    `cost` INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE `Resource` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `name` VARCHAR(191) NOT NULL,
    `type` ENUM('F', 'W', 'L') NOT NULL,

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

-- CreateTable
CREATE TABLE `Deployment` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `type` ENUM('S', 'A') NOT NULL,
    `anthill_aggressor` INTEGER NOT NULL,
    `anthill_defensor` INTEGER NOT NULL,
    `init` DATETIME(3) NOT NULL,
    `finish` DATETIME(3) NOT NULL,
    `result` JSON NULL,

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

-- CreateTable
CREATE TABLE `ants_deploy` (
    `ants` INTEGER NOT NULL,
    `deployment` INTEGER NOT NULL,
    `quantity` INTEGER NOT NULL,

    PRIMARY KEY (`ants`, `deployment`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `construction_anthill` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `anthill` INTEGER NOT NULL,
    `construction` INTEGER NOT NULL,

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

-- CreateTable
CREATE TABLE `investigation_anthill` (
    `anthill` INTEGER NOT NULL,
    `investigation` INTEGER NOT NULL,

    PRIMARY KEY (`anthill`, `investigation`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `ants_anthill` (
    `ant` INTEGER NOT NULL,
    `anthill` INTEGER NOT NULL,
    `total` INTEGER NOT NULL,
    `busy` INTEGER NOT NULL,

    PRIMARY KEY (`ant`, `anthill`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `exploration` (
    `anthill` INTEGER NOT NULL,
    `resource_type` INTEGER NOT NULL,
    `ants` INTEGER NOT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `duration` INTEGER NOT NULL,
    `quantity` INTEGER NOT NULL,

    PRIMARY KEY (`anthill`, `resource_type`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `resource_anthill` (
    `anthill` INTEGER NOT NULL,
    `resource` INTEGER NOT NULL,
    `stock` INTEGER NOT NULL,

    PRIMARY KEY (`anthill`, `resource`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `Requirement` (
    `id` INTEGER NOT NULL AUTO_INCREMENT,
    `item` INTEGER NOT NULL,
    `type_item` ENUM('a', 'c', 'i') NOT NULL,
    `resource_id` INTEGER NOT NULL,
    `resource_type` ENUM('F', 'W', 'L') NOT NULL,
    `resource` INTEGER NOT NULL,

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

-- AddForeignKey
ALTER TABLE `Anthill` ADD CONSTRAINT `Anthill_owner_id_fkey` FOREIGN KEY (`owner_id`) REFERENCES `User`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Deployment` ADD CONSTRAINT `Deployment_anthill_aggressor_fkey` FOREIGN KEY (`anthill_aggressor`) REFERENCES `Anthill`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `Deployment` ADD CONSTRAINT `Deployment_anthill_defensor_fkey` FOREIGN KEY (`anthill_defensor`) REFERENCES `Anthill`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `ants_deploy` ADD CONSTRAINT `ants_deploy_ants_fkey` FOREIGN KEY (`ants`) REFERENCES `Ant`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `ants_deploy` ADD CONSTRAINT `ants_deploy_deployment_fkey` FOREIGN KEY (`deployment`) REFERENCES `Deployment`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `construction_anthill` ADD CONSTRAINT `construction_anthill_anthill_fkey` FOREIGN KEY (`anthill`) REFERENCES `Anthill`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `construction_anthill` ADD CONSTRAINT `construction_anthill_construction_fkey` FOREIGN KEY (`construction`) REFERENCES `Construction`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `investigation_anthill` ADD CONSTRAINT `investigation_anthill_anthill_fkey` FOREIGN KEY (`anthill`) REFERENCES `Anthill`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `investigation_anthill` ADD CONSTRAINT `investigation_anthill_investigation_fkey` FOREIGN KEY (`investigation`) REFERENCES `Investigation`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `ants_anthill` ADD CONSTRAINT `ants_anthill_ant_fkey` FOREIGN KEY (`ant`) REFERENCES `Ant`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `ants_anthill` ADD CONSTRAINT `ants_anthill_anthill_fkey` FOREIGN KEY (`anthill`) REFERENCES `Anthill`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `exploration` ADD CONSTRAINT `exploration_anthill_fkey` FOREIGN KEY (`anthill`) REFERENCES `Anthill`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `exploration` ADD CONSTRAINT `exploration_resource_type_fkey` FOREIGN KEY (`resource_type`) REFERENCES `Resource`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `resource_anthill` ADD CONSTRAINT `resource_anthill_anthill_fkey` FOREIGN KEY (`anthill`) REFERENCES `Anthill`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `resource_anthill` ADD CONSTRAINT `resource_anthill_resource_fkey` FOREIGN KEY (`resource`) REFERENCES `Resource`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
