-- name: CreateAccount :one
INSERT INTO accounts
(name, budget_id)
VALUES ($1, $2)
RETURNING *;

-- name: GetAccount :one
SELECT accounts.* FROM accounts
WHERE accounts.id = $1;

-- name: GetAccounts :many
SELECT accounts.* FROM accounts
WHERE accounts.budget_id = $1
ORDER BY accounts.name;

-- name: GetAccountsWithBalance :many
SELECT accounts.id, accounts.name, accounts.on_budget, SUM(transactions.amount)::decimal(12,2) as balance
FROM accounts
LEFT JOIN transactions ON transactions.account_id = accounts.id AND transactions.date < NOW()
WHERE accounts.budget_id = $1
GROUP BY accounts.id, accounts.name
ORDER BY accounts.name;

-- name: SearchAccounts :many
SELECT accounts.id, accounts.budget_id, accounts.name, true as is_account FROM accounts
WHERE accounts.budget_id = @budget_id
AND accounts.name LIKE @search
ORDER BY accounts.name;