-- name: GetTransaction :one
SELECT * FROM transactions
WHERE id = $1;

-- name: CreateTransaction :one
INSERT INTO transactions
(date, memo, amount, account_id, payee_id, category_id, group_id, status)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING *;

-- name: UpdateTransaction :exec
UPDATE transactions
SET date = $1,
    memo = $2,
    amount = $3,
    account_id = $4,
    payee_id = $5,
    category_id = $6
WHERE id = $7;

-- name: DeleteTransaction :exec
DELETE FROM transactions
WHERE id = $1;

-- name: GetAllTransactionsForBudget :many
SELECT  transactions.id, transactions.date, transactions.memo, 
        transactions.amount, transactions.group_id, transactions.status,
        accounts.name as account, 
        COALESCE(payees.name, '') as payee, 
        COALESCE(category_groups.name, '') as category_group, 
        COALESCE(categories.name, '') as category,
        COALESCE((
            SELECT CONCAT(otherAccounts.name) 
            FROM transactions otherTransactions 
            LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id
            WHERE otherTransactions.group_id = transactions.group_id 
            AND otherTransactions.id != transactions.id
        ), '')::text as transfer_account
FROM transactions 
INNER JOIN accounts ON accounts.id = transactions.account_id
LEFT JOIN payees ON payees.id = transactions.payee_id
LEFT JOIN categories ON categories.id = transactions.category_id
LEFT JOIN category_groups ON category_groups.id = categories.category_group_id
WHERE accounts.budget_id = $1
ORDER BY transactions.date DESC;

-- name: GetTransactionsForAccount :many
SELECT  transactions.id, transactions.date, transactions.memo, 
        transactions.amount, transactions.group_id, transactions.status,
        accounts.name as account, 
        COALESCE(payees.name, '') as payee, 
        COALESCE(category_groups.name, '') as category_group, 
        COALESCE(categories.name, '') as category,
        COALESCE((
            SELECT CONCAT(otherAccounts.name) 
            FROM transactions otherTransactions 
            LEFT JOIN accounts otherAccounts ON otherAccounts.id = otherTransactions.account_id
            WHERE otherTransactions.group_id = transactions.group_id 
            AND otherTransactions.id != transactions.id
        ), '')::text as transfer_account
FROM transactions 
INNER JOIN accounts ON accounts.id = transactions.account_id
LEFT JOIN payees ON payees.id = transactions.payee_id
LEFT JOIN categories ON categories.id = transactions.category_id
LEFT JOIN category_groups ON category_groups.id = categories.category_group_id
WHERE transactions.account_id = $1
ORDER BY transactions.date DESC
LIMIT 200;

-- name: DeleteAllTransactions :execrows
DELETE FROM transactions
USING accounts
WHERE accounts.budget_id = @budget_id
AND accounts.id = transactions.account_id;

-- name: GetTransactionsByMonthAndCategory :many
SELECT *
FROM transactions_by_month
WHERE transactions_by_month.budget_id = @budget_id;