-- name: CreateEvent :one
INSERT INTO events (tablo_id, title, event_date, start_time, end_time, description, location)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, tablo_id, title, event_date, start_time, end_time, description, location, created_at, updated_at;

-- name: GetEventByID :one
SELECT id, tablo_id, title, event_date, start_time, end_time, description, location, created_at, updated_at
FROM events
WHERE id = $1 AND tablo_id = $2;

-- name: UpdateEvent :one
UPDATE events
SET title = $3,
    event_date = $4,
    start_time = $5,
    end_time = $6,
    description = $7,
    location = $8,
    updated_at = now()
WHERE id = $1 AND tablo_id = $2
RETURNING id, tablo_id, title, event_date, start_time, end_time, description, location, created_at, updated_at;

-- name: DeleteEvent :exec
DELETE FROM events
WHERE id = $1 AND tablo_id = $2;

-- name: ListUserEventsRange :many
SELECT events.id,
       events.tablo_id,
       events.title,
       events.event_date,
       events.start_time,
       events.end_time,
       events.description,
       events.location,
       events.created_at,
       events.updated_at,
       tablos.title AS tablo_title,
       tablos.color AS tablo_color
FROM events
JOIN tablos ON tablos.id = events.tablo_id
WHERE tablos.user_id = sqlc.arg(user_id)
  AND events.event_date >= sqlc.arg(start_date)
  AND events.event_date <= sqlc.arg(end_date)
ORDER BY events.event_date, events.start_time, events.title;

-- name: ListEventsByTabloRange :many
SELECT id, tablo_id, title, event_date, start_time, end_time, description, location, created_at, updated_at
FROM events
WHERE tablo_id = $1
  AND event_date >= $2
  AND event_date <= $3
ORDER BY event_date, start_time, title;
