-- 0004_grocy_full.sql - Grocy 主数据同步字段 (MySQL)
ALTER TABLE chemicals
    ADD COLUMN description          TEXT             DEFAULT NULL,
    ADD COLUMN current_amount       DOUBLE          NOT NULL DEFAULT 0,
    ADD COLUMN current_value        DOUBLE          NOT NULL DEFAULT 0,
    ADD COLUMN min_stock_amount     DOUBLE          NOT NULL DEFAULT 0,
    ADD COLUMN best_before_date     VARCHAR(20)      DEFAULT NULL,
    ADD COLUMN location             VARCHAR(255)     DEFAULT NULL,
    ADD COLUMN product_group_id     INT              DEFAULT NULL,
    ADD COLUMN qu_id                INT              DEFAULT NULL,
    ADD COLUMN location_id          INT              DEFAULT NULL,
    ADD COLUMN picture_file_name    VARCHAR(255)     DEFAULT NULL,
    ADD COLUMN last_synced_at       DATETIME         DEFAULT NULL;

CREATE INDEX idx_chem_amount  ON chemicals(current_amount);
CREATE INDEX idx_chem_pg     ON chemicals(product_group_id);
CREATE INDEX idx_chem_synced ON chemicals(last_synced_at);

INSERT IGNORE INTO settings (`key`, value, is_secret, description) VALUES
    ('grocy_sync_batch',     '50',  0, 'Grocy 扣减同步每批条数'),
    ('grocy_low_stock_pct',  '20',  0, '低库存阈值（百分比）'),
    ('grocy_pull_auto',      '0',   0, 'Grocy 拉取模式：0=手动，1=启动时自动拉');
