-- =============================================================================
-- Migration 0014: Grocy 鉴权改造 + 同步日志表 + 天气默认城市
-- =============================================================================

-- 1. settings 表：新增 grocy_username / grocy_password
INSERT OR IGNORE INTO settings (key, value, is_secret, description) VALUES
    ('grocy_username', '', 1, 'Grocy 用户名（session cookie 鉴权）'),
    ('grocy_password', '', 1, 'Grocy 密码（session cookie 鉴权）');

-- 2. 新增默认城市设置
INSERT OR IGNORE INTO settings (key, value, is_secret, description) VALUES
    ('app_city_default', '', 0, '天气默认城市（永久生效）');

-- 3. Grocy 同步日志表
CREATE TABLE IF NOT EXISTS grocy_sync_logs (
    id          INTEGER PRIMARY KEY AUTOINCREMENT,
    action      TEXT NOT NULL,          -- 'pull_products' | 'sync_usage'
    status      TEXT NOT NULL,          -- 'success' | 'failed' | 'partial'
    ok_count    INTEGER NOT NULL DEFAULT 0,
    fail_count  INTEGER NOT NULL DEFAULT 0,
    detail      TEXT,                   -- JSON 详情
    started_at  TEXT NOT NULL DEFAULT (datetime('now')),
    finished_at TEXT
);
CREATE INDEX IF NOT EXISTS idx_grocy_sync_logs_action ON grocy_sync_logs(action);
CREATE INDEX IF NOT EXISTS idx_grocy_sync_logs_started ON grocy_sync_logs(started_at DESC);
