-- name: GetAddress :one
SELECT * FROM addresses
WHERE id = ? LIMIT 1;

-- name: ListAddresses :many
SELECT * FROM addresses
ORDER BY name
LIMIT ? OFFSET ?;

-- name: ListAddressesByCustomer :many
SELECT * FROM addresses
WHERE customer_id = ?
ORDER BY name;

-- name: CreateAddress :execresult
INSERT INTO addresses (
    name, address, city, state_id, country_id, 
    customer_id, type, postcode
) VALUES (
    ?, ?, ?, ?, ?, ?, ?, ?
);

-- name: UpdateAddress :exec
UPDATE addresses
SET name = ?,
    address = ?,
    city = ?,
    state_id = ?,
    country_id = ?,
    customer_id = ?,
    type = ?,
    postcode = ?
WHERE id = ?;

-- name: DeleteAddress :exec
DELETE FROM addresses
WHERE id = ?;

-- name: GetCustomerAddresses :many
SELECT a.*, s.name as state_name, s.shortform as state_shortform, c.name as country_name
FROM addresses a
LEFT JOIN states s ON a.state_id = s.id
LEFT JOIN countries c ON a.country_id = c.id
WHERE a.customer_id = ?
ORDER BY a.name;