-- =============================================================================
-- 洗车记录系统 - Migration 0004: Grocy 主数据同步字段
-- =============================================================================

-- 1. chemicals 表加 Grocy 完整字段
ALTER TABLE chemicals ADD COLUMN description          TEXT;
ALTER TABLE chemicals ADD COLUMN current_amount       REAL NOT NULL DEFAULT 0;
ALTER TABLE chemicals ADD COLUMN current_value        REAL NOT NULL DEFAULT 0;
ALTER TABLE chemicals ADD COLUMN min_stock_amount     REAL NOT NULL DEFAULT 0;
ALTER TABLE chemicals ADD COLUMN best_before_date     TEXT;
ALTER TABLE chemicals ADD COLUMN location             TEXT;
ALTER TABLE chemicals ADD COLUMN product_group_id     INTEGER;
ALTER TABLE chemicals ADD COLUMN qu_id                INTEGER;
ALTER TABLE chemicals ADD COLUMN location_id          INTEGER;
ALTER TABLE chemicals ADD COLUMN picture_file_name    TEXT;
ALTER TABLE chemicals ADD COLUMN last_synced_at       TEXT;

-- 2. 索引
CREATE INDEX IF NOT EXISTS idx_chem_amount      ON chemicals(current_amount);
CREATE INDEX IF NOT EXISTS idx_chem_pg          ON chemicals(product_group_id);
CREATE INDEX IF NOT EXISTS idx_chem_synced      ON chemicals(last_synced_at);

-- 3. Grocy 设置 seed
INSERT OR IGNORE INTO settings (key, value, is_secret, description) VALUES
    ('grocy_sync_batch',     '50',  0, 'Grocy 扣减同步每批条数'),
    ('grocy_low_stock_pct',  '20',  0, '低库存阈值（百分比，库存/min_stock_amount * 100 <= 该值时标红）'),
    ('grocy_pull_auto',      '0',   0, 'Grocy 拉取模式：0=手动，1=启动时自动拉');
