-- +goose Up
-- MySQL 方言初始迁移：providers、models、usage_stats 完整表结构

CREATE TABLE IF NOT EXISTS providers (
    id VARCHAR(36) PRIMARY KEY,
    name VARCHAR(255) NOT NULL,
    api_key VARCHAR(255) NOT NULL,
    base_url VARCHAR(255) NOT NULL,
    protocol VARCHAR(50) DEFAULT 'openai',
    enabled BOOLEAN DEFAULT TRUE,
    created_at DATETIME(3),
    updated_at DATETIME(3)
);

CREATE TABLE IF NOT EXISTS models (
    id VARCHAR(36) PRIMARY KEY,
    provider_id VARCHAR(36) NOT NULL,
    model_name VARCHAR(255) NOT NULL,
    enabled BOOLEAN DEFAULT TRUE,
    created_at DATETIME(3),
    FOREIGN KEY (provider_id) REFERENCES providers(id) ON DELETE CASCADE,
    UNIQUE(provider_id, model_name)
);

CREATE TABLE IF NOT EXISTS usage_stats (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    provider_id VARCHAR(36) NOT NULL,
    model_name VARCHAR(255) NOT NULL,
    request_count INT DEFAULT 0,
    date DATE NOT NULL,
    UNIQUE(provider_id, model_name, date)
);

CREATE INDEX idx_models_provider_id ON models(provider_id);
CREATE INDEX idx_models_model_name ON models(model_name);
CREATE INDEX idx_usage_stats_provider_model_date ON usage_stats(provider_id, model_name, date);

-- +goose Down
DROP INDEX IF EXISTS idx_usage_stats_provider_model_date ON usage_stats;
DROP INDEX IF EXISTS idx_models_model_name ON models;
DROP INDEX IF EXISTS idx_models_provider_id ON models;
DROP TABLE IF EXISTS usage_stats;
DROP TABLE IF EXISTS models;
DROP TABLE IF EXISTS providers;
