-- name: GetCreditBalance :one SELECT * FROM credit_balances WHERE company_id = $1 LIMIT 1; -- name: UpsertCreditBalance :one INSERT INTO credit_balances (company_id, total_credits, used_credits, updated_at) VALUES ($1, $2, $3, now()) ON CONFLICT (company_id) DO UPDATE SET total_credits = EXCLUDED.total_credits, used_credits = EXCLUDED.used_credits, updated_at = now() RETURNING *; -- name: GetActiveCompanyPlan :one SELECT cp.*, p.name AS plan_name, p.monthly_credits, p.max_products FROM company_plans cp JOIN plans p ON p.id = cp.plan_id WHERE cp.company_id = $1 AND cp.is_active = true ORDER BY cp.created_at DESC LIMIT 1; -- name: ListPlans :many SELECT * FROM plans ORDER BY id; -- name: CreatePlan :one INSERT INTO plans (name, description, monthly_credits, yearly_credits, max_products, is_custom, term) VALUES ($1, $2, $3, $4, $5, $6, $7) RETURNING *; -- name: CreateCompanyPlan :one INSERT INTO company_plans ( company_id, plan_id, is_active, billing_cycle_start, next_billing_date, is_trial, trial_ends_at, trial_credits ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8) RETURNING *;