-- name: CreateUser :one
INSERT INTO users (id, email, password_hash, timezone)
VALUES ($1, $2, $3, $4)
RETURNING id, email, password_hash, timezone, is_active, created_at, updated_at;

-- name: GetUserByID :one
SELECT id, email, password_hash, timezone, is_active, week_start_day, date_format, time_format,
       default_event_duration_minutes, default_reminder_minutes, show_weekends,
       working_hours_start, working_hours_end, notifications_email, created_at, updated_at
FROM users
WHERE id = $1 AND deleted_at IS NULL;

-- name: GetUserByEmail :one
SELECT id, email, password_hash, timezone, is_active, week_start_day, date_format, time_format,
       default_event_duration_minutes, default_reminder_minutes, show_weekends,
       working_hours_start, working_hours_end, notifications_email, created_at, updated_at
FROM users
WHERE email = $1 AND deleted_at IS NULL;

-- name: UpdateUser :one
UPDATE users
SET timezone = COALESCE(sqlc.narg('timezone')::TEXT, timezone),
    week_start_day = COALESCE(sqlc.narg('week_start_day')::SMALLINT, week_start_day),
    date_format = COALESCE(sqlc.narg('date_format')::TEXT, date_format),
    time_format = COALESCE(sqlc.narg('time_format')::TEXT, time_format),
    default_event_duration_minutes = COALESCE(sqlc.narg('default_event_duration_minutes')::INTEGER, default_event_duration_minutes),
    default_reminder_minutes = COALESCE(sqlc.narg('default_reminder_minutes')::INTEGER, default_reminder_minutes),
    show_weekends = COALESCE(sqlc.narg('show_weekends')::BOOLEAN, show_weekends),
    working_hours_start = COALESCE(sqlc.narg('working_hours_start')::TEXT, working_hours_start),
    working_hours_end = COALESCE(sqlc.narg('working_hours_end')::TEXT, working_hours_end),
    notifications_email = COALESCE(sqlc.narg('notifications_email')::BOOLEAN, notifications_email),
    updated_at = now()
WHERE id = @id AND deleted_at IS NULL
RETURNING id, email, password_hash, timezone, is_active, week_start_day, date_format, time_format,
          default_event_duration_minutes, default_reminder_minutes, show_weekends,
          working_hours_start, working_hours_end, notifications_email, created_at, updated_at;

-- name: SoftDeleteUser :exec
UPDATE users SET deleted_at = now(), is_active = false, updated_at = now()
WHERE id = $1 AND deleted_at IS NULL;
