-- AlterTable
ALTER TABLE `users` ADD COLUMN `district` VARCHAR(80) NULL;

-- CreateTable
CREATE TABLE `employee_role_masters` (
    `id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(80) NOT NULL,
    `description` VARCHAR(255) NULL,
    `is_active` BOOLEAN NOT NULL DEFAULT true,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

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

-- CreateTable
CREATE TABLE `employee_designations` (
    `id` VARCHAR(191) NOT NULL,
    `role_id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(80) NOT NULL,
    `is_active` BOOLEAN NOT NULL DEFAULT true,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

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

-- CreateTable
CREATE TABLE `employee_profiles` (
    `id` VARCHAR(191) NOT NULL,
    `user_id` VARCHAR(191) NOT NULL,
    `employee_code` VARCHAR(20) NOT NULL,
    `role_id` VARCHAR(191) NOT NULL,
    `designation_id` VARCHAR(191) NOT NULL,
    `aadhaar_url` VARCHAR(500) NULL,
    `created_by_id` VARCHAR(191) NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    UNIQUE INDEX `employee_profiles_user_id_key`(`user_id`),
    UNIQUE INDEX `employee_profiles_employee_code_key`(`employee_code`),
    INDEX `employee_profiles_role_id_idx`(`role_id`),
    INDEX `employee_profiles_designation_id_idx`(`designation_id`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `employee_menu_access` (
    `id` VARCHAR(191) NOT NULL,
    `employee_profile_id` VARCHAR(191) NOT NULL,
    `menu_key` ENUM('USERS', 'DEALERS', 'LISTINGS', 'LEADS', 'TEST_DRIVES', 'LOANS', 'MODERATION', 'CONTACT_US') NOT NULL,
    `can_view` BOOLEAN NOT NULL DEFAULT false,
    `can_add` BOOLEAN NOT NULL DEFAULT false,
    `can_edit` BOOLEAN NOT NULL DEFAULT false,
    `can_delete` BOOLEAN NOT NULL DEFAULT false,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updated_at` DATETIME(3) NOT NULL,

    UNIQUE INDEX `employee_menu_access_employee_profile_id_menu_key_key`(`employee_profile_id`, `menu_key`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE `employee_city_access` (
    `id` VARCHAR(191) NOT NULL,
    `employee_profile_id` VARCHAR(191) NOT NULL,
    `state` VARCHAR(80) NOT NULL,
    `district` VARCHAR(80) NULL,
    `city` VARCHAR(80) NOT NULL,
    `created_at` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

    UNIQUE INDEX `employee_city_access_employee_profile_id_state_district_city_key`(`employee_profile_id`, `state`, `district`, `city`),
    INDEX `employee_city_access_state_city_idx`(`state`, `city`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- AddForeignKey
ALTER TABLE `employee_designations` ADD CONSTRAINT `employee_designations_role_id_fkey` FOREIGN KEY (`role_id`) REFERENCES `employee_role_masters`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `employee_profiles` ADD CONSTRAINT `employee_profiles_user_id_fkey` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `employee_profiles` ADD CONSTRAINT `employee_profiles_role_id_fkey` FOREIGN KEY (`role_id`) REFERENCES `employee_role_masters`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `employee_profiles` ADD CONSTRAINT `employee_profiles_designation_id_fkey` FOREIGN KEY (`designation_id`) REFERENCES `employee_designations`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `employee_profiles` ADD CONSTRAINT `employee_profiles_created_by_id_fkey` FOREIGN KEY (`created_by_id`) REFERENCES `users`(`id`) ON DELETE SET NULL ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `employee_menu_access` ADD CONSTRAINT `employee_menu_access_employee_profile_id_fkey` FOREIGN KEY (`employee_profile_id`) REFERENCES `employee_profiles`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;

-- AddForeignKey
ALTER TABLE `employee_city_access` ADD CONSTRAINT `employee_city_access_employee_profile_id_fkey` FOREIGN KEY (`employee_profile_id`) REFERENCES `employee_profiles`(`id`) ON DELETE CASCADE ON UPDATE CASCADE;
