CREATE TABLE subscription (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    -- Who is subscribed: "user" or "project"
    scope VARCHAR(20) NOT NULL,
    scope_id UUID NOT NULL,
    -- Subscription period
    start_at TIMESTAMPTZ NOT NULL,
    end_at TIMESTAMPTZ NOT NULL,
    -- Payment info
    order_id VARCHAR(100),
    platform VARCHAR(50) NOT NULL,
    plan_name VARCHAR(100),
    -- Quotas
    quota_6h DECIMAL(20, 4) DEFAULT 0 NOT NULL,
    quota_6h_used DECIMAL(20, 4) DEFAULT 0 NOT NULL,
    quota_6h_reset_at TIMESTAMPTZ,
    quota_weekly DECIMAL(20, 4) DEFAULT 0 NOT NULL,
    quota_weekly_used DECIMAL(20, 4) DEFAULT 0 NOT NULL,
    quota_weekly_reset_at TIMESTAMPTZ,
    quota_monthly DECIMAL(20, 4) DEFAULT 0 NOT NULL,
    quota_monthly_used DECIMAL(20, 4) DEFAULT 0 NOT NULL,
    -- Status
    is_active BOOLEAN DEFAULT TRUE NOT NULL,
    currency VARCHAR(10) DEFAULT 'USD' NOT NULL,
    updated_at TIMESTAMPTZ DEFAULT NOW(),
    created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX idx_subscription_scope ON subscription (scope, scope_id);
CREATE INDEX idx_subscription_active ON subscription (is_active) WHERE is_active = TRUE;
CREATE INDEX idx_subscription_end_at ON subscription (end_at) WHERE is_active = TRUE;