CREATE TABLE ai_point_usage_logs ( id BIGSERIAL PRIMARY KEY, tenant_id BIGINT NOT NULL REFERENCES tenants(id), operator_id BIGINT REFERENCES users(id), quota_reservation_id BIGINT REFERENCES quota_reservations(id), usage_type VARCHAR(64) NOT NULL, resource_type VARCHAR(64), resource_id BIGINT, resource_uid VARCHAR(128), request_chars INT NOT NULL DEFAULT 0, base_chars INT NOT NULL DEFAULT 1000, points INT NOT NULL, status VARCHAR(20) NOT NULL DEFAULT 'pending', model VARCHAR(128), metadata_json JSONB NOT NULL DEFAULT '{}'::jsonb, error_message TEXT, completed_at TIMESTAMPTZ, created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), CONSTRAINT chk_ai_point_usage_status CHECK (status IN ('pending', 'completed', 'refunded', 'failed')) ); CREATE INDEX idx_ai_point_usage_tenant_created ON ai_point_usage_logs(tenant_id, created_at DESC); CREATE INDEX idx_ai_point_usage_tenant_operator_created ON ai_point_usage_logs(tenant_id, operator_id, created_at DESC); CREATE INDEX idx_ai_point_usage_reservation ON ai_point_usage_logs(quota_reservation_id); CREATE INDEX idx_ai_point_usage_resource ON ai_point_usage_logs(tenant_id, resource_type, resource_id, resource_uid); CREATE INDEX idx_quota_reservations_tenant_type_created_status ON quota_reservations(tenant_id, quota_type, created_at, status);