-- 添加订单表和用户订阅到期时间字段
-- 执行时间: 2026-02

-- 1. 为 users 表添加 subscription_expires_at 字段
ALTER TABLE users ADD COLUMN IF NOT EXISTS subscription_expires_at TIMESTAMP WITH TIME ZONE DEFAULT NULL;

-- 2. 创建 orders 表
CREATE TABLE IF NOT EXISTS orders (
    id VARCHAR NOT NULL PRIMARY KEY,
    user_id VARCHAR NOT NULL REFERENCES users(id),
    plan_id VARCHAR NOT NULL,
    plan_name VARCHAR NOT NULL,
    amount INTEGER NOT NULL,
    currency VARCHAR DEFAULT 'CNY',
    payment_method VARCHAR NOT NULL,
    status VARCHAR DEFAULT 'pending',
    trade_no VARCHAR,
    paid_at TIMESTAMP WITH TIME ZONE,
    created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
    expired_at TIMESTAMP WITH TIME ZONE
);

-- 3. 创建索引
CREATE INDEX IF NOT EXISTS ix_orders_user_id ON orders(user_id);
CREATE INDEX IF NOT EXISTS ix_orders_trade_no ON orders(trade_no);
CREATE INDEX IF NOT EXISTS ix_orders_status ON orders(status);
