-- name: CreateSubscription :one
INSERT INTO billing.subscriptions (billing_account_id, status, current_period_start, current_period_end)
VALUES ($1, $2, $3, $4)
RETURNING *;

-- name: GetSubscriptionByID :one
SELECT * FROM billing.subscriptions
WHERE subscription_id = $1;

-- name: GetSubscriptionsByBillingAccountID :many
SELECT * FROM billing.subscriptions
WHERE billing_account_id = $1
ORDER BY created_at DESC;

-- name: UpdateSubscriptionStatus :one
UPDATE billing.subscriptions
SET status = $2,
    current_period_start = $3,
    current_period_end = $4,
    cancel_at_period_end = $5,
    canceled_at = $6,
    ended_at = $7
WHERE subscription_id = $1
RETURNING *;

-- name: ListSubscriptions :many
SELECT 
    s.subscription_id,
    s.billing_account_id,
    s.status,
    s.current_period_start,
    s.current_period_end,
    s.cancel_at_period_end,
    s.canceled_at,
    s.ended_at,
    s.created_at,
    s.updated_at,
    ba.name as billing_account_name
FROM billing.subscriptions s
JOIN billing.accounts ba ON s.billing_account_id = ba.billing_account_id
ORDER BY s.created_at DESC;
