CREATE TABLE logical_groups (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    logical_group_id TEXT NOT NULL UNIQUE,
    display_name TEXT NOT NULL,
    status TEXT NOT NULL,
    description TEXT NOT NULL DEFAULT '',
    route_policy TEXT NOT NULL DEFAULT 'priority',
    sticky_mode TEXT NOT NULL DEFAULT 'conversation_preferred',
    conversation_ttl_seconds INTEGER NOT NULL DEFAULT 7200,
    user_model_ttl_seconds INTEGER NOT NULL DEFAULT 1800,
    failover_threshold INTEGER NOT NULL DEFAULT 2,
    cooldown_seconds INTEGER NOT NULL DEFAULT 600,
    created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_logical_groups_status ON logical_groups(status);

CREATE TABLE logical_group_models (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    logical_group_id TEXT NOT NULL,
    public_model TEXT NOT NULL,
    status TEXT NOT NULL DEFAULT 'active',
    created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_logical_group_models_group
        FOREIGN KEY (logical_group_id)
        REFERENCES logical_groups(logical_group_id)
        ON DELETE CASCADE,
    CONSTRAINT uq_logical_group_models_group_model
        UNIQUE (logical_group_id, public_model)
);

CREATE INDEX idx_logical_group_models_group_id ON logical_group_models(logical_group_id);
CREATE INDEX idx_logical_group_models_status ON logical_group_models(status);

CREATE TABLE logical_group_routes (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    route_id TEXT NOT NULL UNIQUE,
    logical_group_id TEXT NOT NULL,
    name TEXT NOT NULL,
    status TEXT NOT NULL,
    priority INTEGER NOT NULL,
    weight INTEGER NOT NULL DEFAULT 100,
    shadow_group_id TEXT NOT NULL,
    shadow_host_id TEXT NOT NULL,
    upstream_base_url_hint TEXT NOT NULL DEFAULT '',
    cooldown_until TEXT NOT NULL DEFAULT '',
    created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_logical_group_routes_group
        FOREIGN KEY (logical_group_id)
        REFERENCES logical_groups(logical_group_id)
        ON DELETE CASCADE
);

CREATE INDEX idx_logical_group_routes_group_id ON logical_group_routes(logical_group_id);
CREATE INDEX idx_logical_group_routes_shadow_host_id ON logical_group_routes(shadow_host_id);
CREATE INDEX idx_logical_group_routes_status_priority ON logical_group_routes(status, priority);

CREATE TABLE logical_group_route_models (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    route_id TEXT NOT NULL,
    public_model TEXT NOT NULL,
    shadow_model TEXT NOT NULL DEFAULT '',
    status TEXT NOT NULL DEFAULT 'active',
    created_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    updated_at TEXT NOT NULL DEFAULT CURRENT_TIMESTAMP,
    CONSTRAINT fk_logical_group_route_models_route
        FOREIGN KEY (route_id)
        REFERENCES logical_group_routes(route_id)
        ON DELETE CASCADE,
    CONSTRAINT uq_logical_group_route_models_route_model
        UNIQUE (route_id, public_model)
);

CREATE INDEX idx_logical_group_route_models_route_id ON logical_group_route_models(route_id);
CREATE INDEX idx_logical_group_route_models_status ON logical_group_route_models(status);
