-- CreateTable
CREATE TABLE IF NOT EXISTS `ModelRoute` (
    `id` VARCHAR(191) NOT NULL,
    `tier` VARCHAR(32) NOT NULL,
    `taskType` VARCHAR(32) NOT NULL DEFAULT '*',
    `preferredProvider` VARCHAR(32) NOT NULL,
    `preferredModel` VARCHAR(100) NOT NULL,
    `fallbackProvider` VARCHAR(32) NOT NULL,
    `fallbackModel` VARCHAR(100) NOT NULL,
    `maxRetries` INTEGER NOT NULL DEFAULT 2,
    `isActive` BOOLEAN NOT NULL DEFAULT true,
    `createdBy` VARCHAR(100) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

    UNIQUE INDEX `ModelRoute_tier_taskType_key`(`tier`, `taskType`),
    PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE IF NOT EXISTS `ProviderConfig` (
    `id` VARCHAR(191) NOT NULL,
    `name` VARCHAR(32) NOT NULL,
    `enabled` BOOLEAN NOT NULL DEFAULT true,
    `baseUrl` VARCHAR(255) NULL,
    `updatedBy` VARCHAR(100) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
    `updatedAt` DATETIME(3) NOT NULL,

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

-- CreateTable
CREATE TABLE IF NOT EXISTS `FallbackEvent` (
    `id` VARCHAR(191) NOT NULL,
    `tier` VARCHAR(32) NOT NULL,
    `taskType` VARCHAR(32) NOT NULL,
    `fromProvider` VARCHAR(32) NOT NULL,
    `fromModel` VARCHAR(100) NOT NULL,
    `toProvider` VARCHAR(32) NOT NULL,
    `toModel` VARCHAR(100) NOT NULL,
    `errorMessage` VARCHAR(500) NULL,
    `createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),

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

-- Seed default routes (skip if already exist)
INSERT IGNORE INTO `ModelRoute` (`id`, `tier`, `taskType`, `preferredProvider`, `preferredModel`, `fallbackProvider`, `fallbackModel`, `maxRetries`) VALUES
('route-cheap-default', 'cheap', '*', 'deepseek', 'deepseek-v4-flash', 'deepseek', 'deepseek-v4-flash', 2),
('route-primary-default', 'primary', '*', 'minimax', 'minimax-m2.7', 'deepseek', 'deepseek-v4-pro', 3),
('route-strong-default', 'strong', '*', 'deepseek', 'deepseek-v4-pro', 'deepseek', 'deepseek-v4-pro', 3);

-- Seed provider configs (skip if already exist)
INSERT IGNORE INTO `ProviderConfig` (`id`, `name`, `enabled`) VALUES
('prov-deepseek', 'deepseek', true),
('prov-minimax', 'minimax', true),
('prov-siliconflow', 'siliconflow', true),
('prov-mock', 'mock', false);
