-- Multi-currency migration for accounting + pricing + llmage
-- Execute against the 'accounting' and 'llmage' databases

-- ============================================================
-- 1. accounting DB: currency + exchange_rate tables
-- ============================================================
USE accounting;

-- currency table
CREATE TABLE IF NOT EXISTS currency (
    id VARCHAR(3) NOT NULL PRIMARY KEY,
    name VARCHAR(32) NOT NULL,
    symbol VARCHAR(8) DEFAULT '',
    decimal_places SMALLINT DEFAULT 2,
    is_base VARCHAR(1) DEFAULT '0',
    status VARCHAR(16) DEFAULT 'active'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

INSERT IGNORE INTO currency (id, name, symbol, decimal_places, is_base, status) VALUES
('CNY', '人民币', '¥', 2, '1', 'active'),
('USD', '美元', '$', 2, '0', 'active'),
('JPY', '日元', '¥', 0, '0', 'active'),
('GBP', '英镑', '£', 2, '0', 'active');

-- exchange_rate table
CREATE TABLE IF NOT EXISTS exchange_rate (
    id VARCHAR(32) NOT NULL PRIMARY KEY,
    from_currency VARCHAR(3) NOT NULL,
    to_currency VARCHAR(3) NOT NULL,
    buy_rate DOUBLE(14,6) DEFAULT 0,
    sell_rate DOUBLE(14,6) DEFAULT 0,
    mid_rate DOUBLE(14,6) DEFAULT 0,
    effective_date DATE NOT NULL,
    updated_at DATETIME DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE UNIQUE INDEX IF NOT EXISTS idx_currency_pair_date
    ON exchange_rate (from_currency, to_currency, effective_date);

INSERT IGNORE INTO exchange_rate (id, from_currency, to_currency, buy_rate, sell_rate, mid_rate, effective_date) VALUES
('er_usd_cny', 'USD', 'CNY', 7.150000, 7.250000, 7.200000, '2025-01-01'),
('er_jpy_cny', 'JPY', 'CNY', 0.048000, 0.049000, 0.048500, '2025-01-01'),
('er_gbp_cny', 'GBP', 'CNY', 9.100000, 9.250000, 9.175000, '2025-01-01'),
('er_cny_usd', 'CNY', 'USD', 0.137900, 0.139900, 0.138900, '2025-01-01');

-- appcodes for currency_status
INSERT IGNORE INTO appcodes (id, name, hierarchy_flg) VALUES ('currency_status', '币种状态', '0');
INSERT IGNORE INTO appcodes_kv (id, parentid, k, v) VALUES
('currency_status_active', 'currency_status', 'active', '启用'),
('currency_status_inactive', 'currency_status', 'inactive', '停用');

-- ============================================================
-- 2. account table: add currency field
-- ============================================================
ALTER TABLE account ADD COLUMN IF NOT EXISTS currency VARCHAR(3) DEFAULT 'CNY' AFTER org1id;

-- Drop old unique index and recreate with currency
-- (manual: if unique index exists on accounting_orgid+orgid+subjectid+org1id, recreate)
-- ALTER TABLE account DROP INDEX idx1;
-- CREATE UNIQUE INDEX idx1 ON account (accounting_orgid, orgid, subjectid, org1id, currency);

-- ============================================================
-- 3. acc_detail / bill / bill_detail / ledger: add currency fields
-- ============================================================
ALTER TABLE acc_detail ADD COLUMN IF NOT EXISTS currency VARCHAR(3) DEFAULT 'CNY' AFTER amount;
ALTER TABLE acc_detail ADD COLUMN IF NOT EXISTS exchange_rate DOUBLE(14,6) DEFAULT 1.0 AFTER currency;
ALTER TABLE acc_detail ADD COLUMN IF NOT EXISTS base_amount DOUBLE(20,2) DEFAULT 0 AFTER exchange_rate;

ALTER TABLE bill ADD COLUMN IF NOT EXISTS currency VARCHAR(3) DEFAULT 'CNY' AFTER amount;
ALTER TABLE bill ADD COLUMN IF NOT EXISTS base_amount DOUBLE(20,2) DEFAULT 0 AFTER currency;

ALTER TABLE bill_detail ADD COLUMN IF NOT EXISTS currency VARCHAR(3) DEFAULT 'CNY' AFTER amount;
ALTER TABLE bill_detail ADD COLUMN IF NOT EXISTS exchange_rate DOUBLE(14,6) DEFAULT 1.0 AFTER currency;
ALTER TABLE bill_detail ADD COLUMN IF NOT EXISTS base_amount DOUBLE(20,2) DEFAULT 0 AFTER exchange_rate;

ALTER TABLE ledger ADD COLUMN IF NOT EXISTS currency VARCHAR(3) DEFAULT 'CNY' AFTER amount;
ALTER TABLE ledger ADD COLUMN IF NOT EXISTS exchange_rate DOUBLE(14,6) DEFAULT 1.0 AFTER currency;
ALTER TABLE ledger ADD COLUMN IF NOT EXISTS base_amount DOUBLE(20,2) DEFAULT 0 AFTER exchange_rate;

-- ============================================================
-- 4. pricing DB: pricing_program add currency
-- ============================================================
ALTER TABLE pricing_program ADD COLUMN IF NOT EXISTS currency VARCHAR(3) DEFAULT 'CNY' AFTER description;

-- ============================================================
-- 5. llmage DB: llmusage add currency fields
-- ============================================================
ALTER TABLE llmusage ADD COLUMN IF NOT EXISTS amount_currency VARCHAR(3) DEFAULT 'CNY' AFTER amount;
ALTER TABLE llmusage ADD COLUMN IF NOT EXISTS amount_base DOUBLE(20,2) DEFAULT 0 AFTER amount_currency;
ALTER TABLE llmusage ADD COLUMN IF NOT EXISTS cost_currency VARCHAR(3) DEFAULT 'CNY' AFTER cost;
ALTER TABLE llmusage ADD COLUMN IF NOT EXISTS cost_base DOUBLE(20,2) DEFAULT 0 AFTER cost_currency;
