-- Phase 2: region_pricing 扩展非 token 统一计费字段（字符/秒等）

ALTER TABLE region_pricing
    ADD COLUMN IF NOT EXISTS pricing_mode TEXT NOT NULL DEFAULT 'input_output',
    ADD COLUMN IF NOT EXISTS price_unit TEXT NOT NULL DEFAULT 'million_tokens',
    ADD COLUMN IF NOT EXISTS flat_price REAL;

DO $$
BEGIN
    IF NOT EXISTS (
        SELECT 1
        FROM pg_constraint
        WHERE conname = 'chk_region_pricing_pricing_mode'
    ) THEN
        ALTER TABLE region_pricing
            ADD CONSTRAINT chk_region_pricing_pricing_mode
            CHECK (pricing_mode IN ('input_output', 'flat'));
    END IF;
END
$$;

UPDATE region_pricing
SET pricing_mode = 'input_output'
WHERE coalesce(pricing_mode, '') = '';

UPDATE region_pricing
SET price_unit = 'million_tokens'
WHERE coalesce(price_unit, '') = '';

CREATE INDEX IF NOT EXISTS idx_region_pricing_pricing_mode ON region_pricing(pricing_mode);
CREATE INDEX IF NOT EXISTS idx_region_pricing_price_unit ON region_pricing(price_unit);
