-- name: CreateUser :one
INSERT INTO users (username, password_hash)
VALUES ($1, $2)
RETURNING *;

-- name: GetUserByUsername :one
SELECT * FROM users WHERE username = $1;

-- name: GetDeliveriesByDate :many
SELECT * FROM deliveries WHERE date = $1;
 
-- name: CreateDelivery :one
INSERT INTO deliveries (
    date, pickup_location, pickup_location_2, product_name, product_name_2,
    customer_name, address, street, house, apartment, entrance, floor,
    phone, additional_phone, has_elevator, service_info, comment
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17)
RETURNING *;

-- name: GetDeliveryByID :one
SELECT * FROM deliveries WHERE id = $1;

-- name: DeleteDelivery :exec
DELETE FROM deliveries WHERE id = $1;

-- name: UpdateDelivery :exec
UPDATE deliveries SET
    date = $1,
    pickup_location = $2,
    pickup_location_2 = $3,
    product_name = $4,
    product_name_2 = $5,
    customer_name = $6,
    address = $7,
    street = $8,
    house = $9,
    apartment = $10,
    entrance = $11,
    floor = $12,
    phone = $13,
    additional_phone = $14,
    has_elevator = $15,
    service_info = $16,
    comment = $17,
    updated_at = NOW()
WHERE id = $18;

-- name: GetDeliveryCount :many
SELECT COUNT(*) as count, date FROM deliveries
WHERE date >= CURRENT_DATE AND date < CURRENT_DATE + INTERVAL '7 days'
GROUP BY date;

-- name: UpdateDeliveryStatus :exec
UPDATE deliveries SET status = $1, updated_at = NOW() WHERE id = $2;
