-- +goose Up
CREATE TABLE IF NOT EXISTS subscriptions (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    url TEXT NOT NULL,
    type TEXT NOT NULL DEFAULT 'clash',
    interval_sec INTEGER NOT NULL DEFAULT 3600,
    enabled INTEGER NOT NULL DEFAULT 1,
    last_fetch_at TEXT,
    last_error TEXT,
    node_count INTEGER NOT NULL DEFAULT 0,
    created_at TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS proxy_nodes (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    subscription_id INTEGER,
    name TEXT NOT NULL,
    runtime_name TEXT NOT NULL UNIQUE,
    type TEXT NOT NULL,
    raw_config TEXT NOT NULL DEFAULT '{}',
    source TEXT NOT NULL DEFAULT 'subscription',
    enabled INTEGER NOT NULL DEFAULT 1,
    latency_ms INTEGER NOT NULL DEFAULT -1,
    alive INTEGER NOT NULL DEFAULT 0,
    checked_at TEXT,
    created_at TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at TEXT NOT NULL DEFAULT (datetime('now')),
    FOREIGN KEY (subscription_id) REFERENCES subscriptions(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS rules (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    priority INTEGER NOT NULL DEFAULT 100,
    rule_type TEXT NOT NULL,
    payload TEXT NOT NULL DEFAULT '',
    target TEXT NOT NULL,
    source TEXT NOT NULL DEFAULT 'manual',
    subscription_id INTEGER,
    enabled INTEGER NOT NULL DEFAULT 1,
    created_at TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at TEXT NOT NULL DEFAULT (datetime('now')),
    FOREIGN KEY (subscription_id) REFERENCES subscriptions(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS proxy_groups (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    name TEXT NOT NULL,
    runtime_name TEXT NOT NULL UNIQUE,
    type TEXT NOT NULL DEFAULT 'select',
    proxies TEXT NOT NULL DEFAULT '[]',
    source TEXT NOT NULL DEFAULT 'subscription',
    subscription_id INTEGER,
    enabled INTEGER NOT NULL DEFAULT 1,
    created_at TEXT NOT NULL DEFAULT (datetime('now')),
    updated_at TEXT NOT NULL DEFAULT (datetime('now')),
    FOREIGN KEY (subscription_id) REFERENCES subscriptions(id) ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS settings (
    key TEXT PRIMARY KEY,
    value TEXT NOT NULL
);

CREATE TABLE IF NOT EXISTS audit_logs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    action TEXT NOT NULL,
    resource TEXT NOT NULL,
    detail TEXT NOT NULL DEFAULT '',
    created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS request_logs (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    level TEXT NOT NULL DEFAULT 'info',
    source TEXT NOT NULL DEFAULT 'prism',
    message TEXT NOT NULL,
    created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

CREATE TABLE IF NOT EXISTS traffic_snapshots (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    upload_rate INTEGER NOT NULL DEFAULT 0,
    download_rate INTEGER NOT NULL DEFAULT 0,
    connections INTEGER NOT NULL DEFAULT 0,
    created_at TEXT NOT NULL DEFAULT (datetime('now'))
);

INSERT OR IGNORE INTO settings (key, value) VALUES
    ('http_port', '7890'),
    ('socks_port', '7891'),
    ('api_port', '8080'),
    ('mihomo_api_port', '9090'),
    ('api_secret', ''),
    ('admin_password', 'admin'),
    ('auth_enabled', 'true'),
    ('log_level', 'info'),
    ('health_interval_sec', '300'),
    ('sub_sync_enabled', 'true'),
    ('default_policy', 'DIRECT'),
    ('geo_auto_download', 'true');

-- +goose Down
DROP TABLE IF EXISTS traffic_snapshots;
DROP TABLE IF EXISTS request_logs;
DROP TABLE IF EXISTS audit_logs;
DROP TABLE IF EXISTS settings;
DROP TABLE IF EXISTS proxy_groups;
DROP TABLE IF EXISTS rules;
DROP TABLE IF EXISTS proxy_nodes;
DROP TABLE IF EXISTS subscriptions;
