-- name: CreatePaymentMethod :one
INSERT INTO billing.payment_methods (
    billing_account_id, pm_type,
    card_brand, card_last4, card_exp_month, card_exp_year,
    funding, is_default
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING *;

-- name: GetPaymentMethodByID :one
SELECT * FROM billing.payment_methods
WHERE payment_method_id = $1;

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

-- name: UpdatePaymentMethodSafeFields :one
UPDATE billing.payment_methods
SET card_brand     = $2,
    card_last4     = $3,
    card_exp_month = $4,
    card_exp_year  = $5,
    funding        = $6
WHERE payment_method_id = $1
RETURNING *;

-- name: MarkPaymentMethodDetached :exec
UPDATE billing.payment_methods
SET detached_at = NOW()
WHERE payment_method_id = $1;
