// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.30.0
// source: store.sql

package db

import (
	"context"
	"database/sql"
	"encoding/json"
	"time"
)

const acceptInvite = `-- name: AcceptInvite :one
UPDATE invites
SET status = 'accepted'
WHERE token = $1
RETURNING id, workspace_slug, email, role, token, created_at, status
`

func (q *Queries) AcceptInvite(ctx context.Context, token string) (Invite, error) {
	row := q.db.QueryRowContext(ctx, acceptInvite, token)
	var i Invite
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Email,
		&i.Role,
		&i.Token,
		&i.CreatedAt,
		&i.Status,
	)
	return i, err
}

const countBoardGroupsByWorkspace = `-- name: CountBoardGroupsByWorkspace :one
SELECT COUNT(*) FROM board_groups WHERE workspace_slug = $1
`

func (q *Queries) CountBoardGroupsByWorkspace(ctx context.Context, workspaceSlug string) (int64, error) {
	row := q.db.QueryRowContext(ctx, countBoardGroupsByWorkspace, workspaceSlug)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const countWorkspaces = `-- name: CountWorkspaces :one
SELECT COUNT(*) FROM workspaces
`

func (q *Queries) CountWorkspaces(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, countWorkspaces)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const createActivity = `-- name: CreateActivity :exec
INSERT INTO activity_entries (id, workspace_slug, title, detail, created_at)
VALUES ($1, $2, $3, $4, $5)
`

type CreateActivityParams struct {
	ID            string
	WorkspaceSlug string
	Title         string
	Detail        string
	CreatedAt     time.Time
}

func (q *Queries) CreateActivity(ctx context.Context, arg CreateActivityParams) error {
	_, err := q.db.ExecContext(ctx, createActivity,
		arg.ID,
		arg.WorkspaceSlug,
		arg.Title,
		arg.Detail,
		arg.CreatedAt,
	)
	return err
}

const createBoardGroup = `-- name: CreateBoardGroup :one
INSERT INTO board_groups (id, workspace_slug, name, color, sort_order)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, workspace_slug, name, color, sort_order
`

type CreateBoardGroupParams struct {
	ID            string
	WorkspaceSlug string
	Name          string
	Color         string
	SortOrder     int32
}

func (q *Queries) CreateBoardGroup(ctx context.Context, arg CreateBoardGroupParams) (BoardGroup, error) {
	row := q.db.QueryRowContext(ctx, createBoardGroup,
		arg.ID,
		arg.WorkspaceSlug,
		arg.Name,
		arg.Color,
		arg.SortOrder,
	)
	var i BoardGroup
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Name,
		&i.Color,
		&i.SortOrder,
	)
	return i, err
}

const createCalendarEvent = `-- name: CreateCalendarEvent :one
INSERT INTO calendar_events (id, workspace_slug, title, description, starts_at, ends_at, color, linked_task_id, attachments)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id, workspace_slug, title, description, starts_at, ends_at, color, linked_task_id, attachments
`

type CreateCalendarEventParams struct {
	ID            string
	WorkspaceSlug string
	Title         string
	Description   string
	StartsAt      time.Time
	EndsAt        time.Time
	Color         string
	LinkedTaskID  sql.NullString
	Attachments   json.RawMessage
}

func (q *Queries) CreateCalendarEvent(ctx context.Context, arg CreateCalendarEventParams) (CalendarEvent, error) {
	row := q.db.QueryRowContext(ctx, createCalendarEvent,
		arg.ID,
		arg.WorkspaceSlug,
		arg.Title,
		arg.Description,
		arg.StartsAt,
		arg.EndsAt,
		arg.Color,
		arg.LinkedTaskID,
		arg.Attachments,
	)
	var i CalendarEvent
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Title,
		&i.Description,
		&i.StartsAt,
		&i.EndsAt,
		&i.Color,
		&i.LinkedTaskID,
		&i.Attachments,
	)
	return i, err
}

const createFocusSession = `-- name: CreateFocusSession :one
INSERT INTO focus_sessions (id, workspace_slug, task_id, mode, started_at, completed_at, paused_at, paused_total_seconds, duration_seconds)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
RETURNING id, workspace_slug, task_id, mode, started_at, completed_at, paused_at, paused_total_seconds, duration_seconds
`

type CreateFocusSessionParams struct {
	ID                 string
	WorkspaceSlug      string
	TaskID             sql.NullString
	Mode               string
	StartedAt          time.Time
	CompletedAt        sql.NullTime
	PausedAt           sql.NullTime
	PausedTotalSeconds int32
	DurationSeconds    int32
}

func (q *Queries) CreateFocusSession(ctx context.Context, arg CreateFocusSessionParams) (FocusSession, error) {
	row := q.db.QueryRowContext(ctx, createFocusSession,
		arg.ID,
		arg.WorkspaceSlug,
		arg.TaskID,
		arg.Mode,
		arg.StartedAt,
		arg.CompletedAt,
		arg.PausedAt,
		arg.PausedTotalSeconds,
		arg.DurationSeconds,
	)
	var i FocusSession
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.TaskID,
		&i.Mode,
		&i.StartedAt,
		&i.CompletedAt,
		&i.PausedAt,
		&i.PausedTotalSeconds,
		&i.DurationSeconds,
	)
	return i, err
}

const createInvite = `-- name: CreateInvite :one
INSERT INTO invites (id, workspace_slug, email, role, token, created_at, status)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, workspace_slug, email, role, token, created_at, status
`

type CreateInviteParams struct {
	ID            string
	WorkspaceSlug string
	Email         string
	Role          string
	Token         string
	CreatedAt     time.Time
	Status        string
}

func (q *Queries) CreateInvite(ctx context.Context, arg CreateInviteParams) (Invite, error) {
	row := q.db.QueryRowContext(ctx, createInvite,
		arg.ID,
		arg.WorkspaceSlug,
		arg.Email,
		arg.Role,
		arg.Token,
		arg.CreatedAt,
		arg.Status,
	)
	var i Invite
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Email,
		&i.Role,
		&i.Token,
		&i.CreatedAt,
		&i.Status,
	)
	return i, err
}

const createLabel = `-- name: CreateLabel :one
INSERT INTO labels (id, workspace_slug, name, color)
VALUES ($1, $2, $3, $4)
RETURNING id, workspace_slug, name, color
`

type CreateLabelParams struct {
	ID            string
	WorkspaceSlug string
	Name          string
	Color         string
}

func (q *Queries) CreateLabel(ctx context.Context, arg CreateLabelParams) (Label, error) {
	row := q.db.QueryRowContext(ctx, createLabel,
		arg.ID,
		arg.WorkspaceSlug,
		arg.Name,
		arg.Color,
	)
	var i Label
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Name,
		&i.Color,
	)
	return i, err
}

const createMember = `-- name: CreateMember :exec
INSERT INTO members (id, workspace_slug, name, email, role, status)
VALUES ($1, $2, $3, $4, $5, $6)
`

type CreateMemberParams struct {
	ID            string
	WorkspaceSlug string
	Name          string
	Email         string
	Role          string
	Status        string
}

func (q *Queries) CreateMember(ctx context.Context, arg CreateMemberParams) error {
	_, err := q.db.ExecContext(ctx, createMember,
		arg.ID,
		arg.WorkspaceSlug,
		arg.Name,
		arg.Email,
		arg.Role,
		arg.Status,
	)
	return err
}

const createNote = `-- name: CreateNote :one
INSERT INTO notes (id, workspace_slug, title, content, updated_at)
VALUES ($1, $2, $3, $4, $5)
RETURNING id, workspace_slug, title, content, updated_at
`

type CreateNoteParams struct {
	ID            string
	WorkspaceSlug string
	Title         string
	Content       string
	UpdatedAt     time.Time
}

func (q *Queries) CreateNote(ctx context.Context, arg CreateNoteParams) (Note, error) {
	row := q.db.QueryRowContext(ctx, createNote,
		arg.ID,
		arg.WorkspaceSlug,
		arg.Title,
		arg.Content,
		arg.UpdatedAt,
	)
	var i Note
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Title,
		&i.Content,
		&i.UpdatedAt,
	)
	return i, err
}

const createTask = `-- name: CreateTask :one
INSERT INTO tasks (id, workspace_slug, board_group_id, title, description, status, color, due_at, scheduled_start, scheduled_end, assignee_id, label_ids, attachments, comments, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16)
RETURNING id, workspace_slug, board_group_id, title, description, status, color, due_at, scheduled_start, scheduled_end, assignee_id, label_ids, attachments, comments, created_at, updated_at
`

type CreateTaskParams struct {
	ID             string
	WorkspaceSlug  string
	BoardGroupID   string
	Title          string
	Description    string
	Status         string
	Color          string
	DueAt          sql.NullTime
	ScheduledStart sql.NullTime
	ScheduledEnd   sql.NullTime
	AssigneeID     sql.NullString
	LabelIds       json.RawMessage
	Attachments    json.RawMessage
	Comments       json.RawMessage
	CreatedAt      time.Time
	UpdatedAt      time.Time
}

func (q *Queries) CreateTask(ctx context.Context, arg CreateTaskParams) (Task, error) {
	row := q.db.QueryRowContext(ctx, createTask,
		arg.ID,
		arg.WorkspaceSlug,
		arg.BoardGroupID,
		arg.Title,
		arg.Description,
		arg.Status,
		arg.Color,
		arg.DueAt,
		arg.ScheduledStart,
		arg.ScheduledEnd,
		arg.AssigneeID,
		arg.LabelIds,
		arg.Attachments,
		arg.Comments,
		arg.CreatedAt,
		arg.UpdatedAt,
	)
	var i Task
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.BoardGroupID,
		&i.Title,
		&i.Description,
		&i.Status,
		&i.Color,
		&i.DueAt,
		&i.ScheduledStart,
		&i.ScheduledEnd,
		&i.AssigneeID,
		&i.LabelIds,
		&i.Attachments,
		&i.Comments,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const createWorkspace = `-- name: CreateWorkspace :exec
INSERT INTO workspaces (id, slug, name, role, created_at)
VALUES ($1, $2, $3, $4, $5)
`

type CreateWorkspaceParams struct {
	ID        string
	Slug      string
	Name      string
	Role      string
	CreatedAt time.Time
}

func (q *Queries) CreateWorkspace(ctx context.Context, arg CreateWorkspaceParams) error {
	_, err := q.db.ExecContext(ctx, createWorkspace,
		arg.ID,
		arg.Slug,
		arg.Name,
		arg.Role,
		arg.CreatedAt,
	)
	return err
}

const getBoardGroupByID = `-- name: GetBoardGroupByID :one
SELECT id, workspace_slug, name, color, sort_order
FROM board_groups
WHERE id = $1
`

func (q *Queries) GetBoardGroupByID(ctx context.Context, id string) (BoardGroup, error) {
	row := q.db.QueryRowContext(ctx, getBoardGroupByID, id)
	var i BoardGroup
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Name,
		&i.Color,
		&i.SortOrder,
	)
	return i, err
}

const getCalendarEventByID = `-- name: GetCalendarEventByID :one
SELECT id, workspace_slug, title, description, starts_at, ends_at, color, linked_task_id, attachments
FROM calendar_events
WHERE id = $1
`

func (q *Queries) GetCalendarEventByID(ctx context.Context, id string) (CalendarEvent, error) {
	row := q.db.QueryRowContext(ctx, getCalendarEventByID, id)
	var i CalendarEvent
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Title,
		&i.Description,
		&i.StartsAt,
		&i.EndsAt,
		&i.Color,
		&i.LinkedTaskID,
		&i.Attachments,
	)
	return i, err
}

const getFocusSessionByID = `-- name: GetFocusSessionByID :one
SELECT id, workspace_slug, task_id, mode, started_at, completed_at, paused_at, paused_total_seconds, duration_seconds
FROM focus_sessions
WHERE id = $1
`

func (q *Queries) GetFocusSessionByID(ctx context.Context, id string) (FocusSession, error) {
	row := q.db.QueryRowContext(ctx, getFocusSessionByID, id)
	var i FocusSession
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.TaskID,
		&i.Mode,
		&i.StartedAt,
		&i.CompletedAt,
		&i.PausedAt,
		&i.PausedTotalSeconds,
		&i.DurationSeconds,
	)
	return i, err
}

const getInviteByToken = `-- name: GetInviteByToken :one
SELECT id, workspace_slug, email, role, token, created_at, status
FROM invites
WHERE token = $1
`

func (q *Queries) GetInviteByToken(ctx context.Context, token string) (Invite, error) {
	row := q.db.QueryRowContext(ctx, getInviteByToken, token)
	var i Invite
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Email,
		&i.Role,
		&i.Token,
		&i.CreatedAt,
		&i.Status,
	)
	return i, err
}

const getMemberByWorkspaceAndEmail = `-- name: GetMemberByWorkspaceAndEmail :one
SELECT id, workspace_slug, name, email, role, status
FROM members
WHERE workspace_slug = $1 AND email = $2
`

type GetMemberByWorkspaceAndEmailParams struct {
	WorkspaceSlug string
	Email         string
}

func (q *Queries) GetMemberByWorkspaceAndEmail(ctx context.Context, arg GetMemberByWorkspaceAndEmailParams) (Member, error) {
	row := q.db.QueryRowContext(ctx, getMemberByWorkspaceAndEmail, arg.WorkspaceSlug, arg.Email)
	var i Member
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Name,
		&i.Email,
		&i.Role,
		&i.Status,
	)
	return i, err
}

const getNoteByID = `-- name: GetNoteByID :one
SELECT id, workspace_slug, title, content, updated_at
FROM notes
WHERE id = $1
`

func (q *Queries) GetNoteByID(ctx context.Context, id string) (Note, error) {
	row := q.db.QueryRowContext(ctx, getNoteByID, id)
	var i Note
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Title,
		&i.Content,
		&i.UpdatedAt,
	)
	return i, err
}

const getTaskByID = `-- name: GetTaskByID :one
SELECT id, workspace_slug, board_group_id, title, description, status, color, due_at, scheduled_start, scheduled_end, assignee_id, label_ids, attachments, comments, created_at, updated_at
FROM tasks
WHERE id = $1
`

func (q *Queries) GetTaskByID(ctx context.Context, id string) (Task, error) {
	row := q.db.QueryRowContext(ctx, getTaskByID, id)
	var i Task
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.BoardGroupID,
		&i.Title,
		&i.Description,
		&i.Status,
		&i.Color,
		&i.DueAt,
		&i.ScheduledStart,
		&i.ScheduledEnd,
		&i.AssigneeID,
		&i.LabelIds,
		&i.Attachments,
		&i.Comments,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listActivities = `-- name: ListActivities :many
SELECT id, workspace_slug, title, detail, created_at
FROM activity_entries
WHERE workspace_slug = $1
ORDER BY created_at DESC
LIMIT 40
`

func (q *Queries) ListActivities(ctx context.Context, workspaceSlug string) ([]ActivityEntry, error) {
	rows, err := q.db.QueryContext(ctx, listActivities, workspaceSlug)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ActivityEntry
	for rows.Next() {
		var i ActivityEntry
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceSlug,
			&i.Title,
			&i.Detail,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listBoardGroups = `-- name: ListBoardGroups :many
SELECT id, workspace_slug, name, color, sort_order
FROM board_groups
WHERE workspace_slug = $1
ORDER BY sort_order ASC, name ASC
`

func (q *Queries) ListBoardGroups(ctx context.Context, workspaceSlug string) ([]BoardGroup, error) {
	rows, err := q.db.QueryContext(ctx, listBoardGroups, workspaceSlug)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BoardGroup
	for rows.Next() {
		var i BoardGroup
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceSlug,
			&i.Name,
			&i.Color,
			&i.SortOrder,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listCalendarEvents = `-- name: ListCalendarEvents :many
SELECT id, workspace_slug, title, description, starts_at, ends_at, color, linked_task_id, attachments
FROM calendar_events
WHERE workspace_slug = $1
ORDER BY starts_at ASC
`

func (q *Queries) ListCalendarEvents(ctx context.Context, workspaceSlug string) ([]CalendarEvent, error) {
	rows, err := q.db.QueryContext(ctx, listCalendarEvents, workspaceSlug)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []CalendarEvent
	for rows.Next() {
		var i CalendarEvent
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceSlug,
			&i.Title,
			&i.Description,
			&i.StartsAt,
			&i.EndsAt,
			&i.Color,
			&i.LinkedTaskID,
			&i.Attachments,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listFocusSessions = `-- name: ListFocusSessions :many
SELECT id, workspace_slug, task_id, mode, started_at, completed_at, paused_at, paused_total_seconds, duration_seconds
FROM focus_sessions
WHERE workspace_slug = $1
ORDER BY started_at DESC
`

func (q *Queries) ListFocusSessions(ctx context.Context, workspaceSlug string) ([]FocusSession, error) {
	rows, err := q.db.QueryContext(ctx, listFocusSessions, workspaceSlug)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []FocusSession
	for rows.Next() {
		var i FocusSession
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceSlug,
			&i.TaskID,
			&i.Mode,
			&i.StartedAt,
			&i.CompletedAt,
			&i.PausedAt,
			&i.PausedTotalSeconds,
			&i.DurationSeconds,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listInvites = `-- name: ListInvites :many
SELECT id, workspace_slug, email, role, token, created_at, status
FROM invites
WHERE workspace_slug = $1
ORDER BY created_at DESC
`

func (q *Queries) ListInvites(ctx context.Context, workspaceSlug string) ([]Invite, error) {
	rows, err := q.db.QueryContext(ctx, listInvites, workspaceSlug)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Invite
	for rows.Next() {
		var i Invite
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceSlug,
			&i.Email,
			&i.Role,
			&i.Token,
			&i.CreatedAt,
			&i.Status,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listLabels = `-- name: ListLabels :many
SELECT id, workspace_slug, name, color
FROM labels
WHERE workspace_slug = $1
ORDER BY name ASC
`

func (q *Queries) ListLabels(ctx context.Context, workspaceSlug string) ([]Label, error) {
	rows, err := q.db.QueryContext(ctx, listLabels, workspaceSlug)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Label
	for rows.Next() {
		var i Label
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceSlug,
			&i.Name,
			&i.Color,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listMembers = `-- name: ListMembers :many
SELECT id, workspace_slug, name, email, role, status
FROM members
WHERE workspace_slug = $1
ORDER BY name ASC
`

func (q *Queries) ListMembers(ctx context.Context, workspaceSlug string) ([]Member, error) {
	rows, err := q.db.QueryContext(ctx, listMembers, workspaceSlug)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Member
	for rows.Next() {
		var i Member
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceSlug,
			&i.Name,
			&i.Email,
			&i.Role,
			&i.Status,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listNotes = `-- name: ListNotes :many
SELECT id, workspace_slug, title, content, updated_at
FROM notes
WHERE workspace_slug = $1
ORDER BY updated_at DESC
`

func (q *Queries) ListNotes(ctx context.Context, workspaceSlug string) ([]Note, error) {
	rows, err := q.db.QueryContext(ctx, listNotes, workspaceSlug)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Note
	for rows.Next() {
		var i Note
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceSlug,
			&i.Title,
			&i.Content,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listTasks = `-- name: ListTasks :many
SELECT id, workspace_slug, board_group_id, title, description, status, color, due_at, scheduled_start, scheduled_end, assignee_id, label_ids, attachments, comments, created_at, updated_at
FROM tasks
WHERE workspace_slug = $1
ORDER BY updated_at DESC
`

func (q *Queries) ListTasks(ctx context.Context, workspaceSlug string) ([]Task, error) {
	rows, err := q.db.QueryContext(ctx, listTasks, workspaceSlug)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Task
	for rows.Next() {
		var i Task
		if err := rows.Scan(
			&i.ID,
			&i.WorkspaceSlug,
			&i.BoardGroupID,
			&i.Title,
			&i.Description,
			&i.Status,
			&i.Color,
			&i.DueAt,
			&i.ScheduledStart,
			&i.ScheduledEnd,
			&i.AssigneeID,
			&i.LabelIds,
			&i.Attachments,
			&i.Comments,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listWorkspaces = `-- name: ListWorkspaces :many
SELECT id, slug, name, role, created_at
FROM workspaces
ORDER BY created_at ASC
`

func (q *Queries) ListWorkspaces(ctx context.Context) ([]Workspace, error) {
	rows, err := q.db.QueryContext(ctx, listWorkspaces)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Workspace
	for rows.Next() {
		var i Workspace
		if err := rows.Scan(
			&i.ID,
			&i.Slug,
			&i.Name,
			&i.Role,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const trimActivities = `-- name: TrimActivities :exec
DELETE FROM activity_entries
WHERE id IN (
  SELECT id
  FROM activity_entries
  WHERE activity_entries.workspace_slug = $1
  ORDER BY created_at DESC
  OFFSET 40
)
`

func (q *Queries) TrimActivities(ctx context.Context, workspaceSlug string) error {
	_, err := q.db.ExecContext(ctx, trimActivities, workspaceSlug)
	return err
}

const updateBoardGroup = `-- name: UpdateBoardGroup :one
UPDATE board_groups
SET name = $2,
    color = $3,
    sort_order = $4
WHERE id = $1
RETURNING id, workspace_slug, name, color, sort_order
`

type UpdateBoardGroupParams struct {
	ID        string
	Name      string
	Color     string
	SortOrder int32
}

func (q *Queries) UpdateBoardGroup(ctx context.Context, arg UpdateBoardGroupParams) (BoardGroup, error) {
	row := q.db.QueryRowContext(ctx, updateBoardGroup,
		arg.ID,
		arg.Name,
		arg.Color,
		arg.SortOrder,
	)
	var i BoardGroup
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Name,
		&i.Color,
		&i.SortOrder,
	)
	return i, err
}

const updateCalendarEvent = `-- name: UpdateCalendarEvent :one
UPDATE calendar_events
SET title = $2,
    description = $3,
    starts_at = $4,
    ends_at = $5,
    color = $6,
    linked_task_id = $7,
    attachments = $8
WHERE id = $1
RETURNING id, workspace_slug, title, description, starts_at, ends_at, color, linked_task_id, attachments
`

type UpdateCalendarEventParams struct {
	ID           string
	Title        string
	Description  string
	StartsAt     time.Time
	EndsAt       time.Time
	Color        string
	LinkedTaskID sql.NullString
	Attachments  json.RawMessage
}

func (q *Queries) UpdateCalendarEvent(ctx context.Context, arg UpdateCalendarEventParams) (CalendarEvent, error) {
	row := q.db.QueryRowContext(ctx, updateCalendarEvent,
		arg.ID,
		arg.Title,
		arg.Description,
		arg.StartsAt,
		arg.EndsAt,
		arg.Color,
		arg.LinkedTaskID,
		arg.Attachments,
	)
	var i CalendarEvent
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Title,
		&i.Description,
		&i.StartsAt,
		&i.EndsAt,
		&i.Color,
		&i.LinkedTaskID,
		&i.Attachments,
	)
	return i, err
}

const updateFocusSession = `-- name: UpdateFocusSession :one
UPDATE focus_sessions
SET completed_at = $2,
    paused_at = $3,
    paused_total_seconds = $4
WHERE id = $1
RETURNING id, workspace_slug, task_id, mode, started_at, completed_at, paused_at, paused_total_seconds, duration_seconds
`

type UpdateFocusSessionParams struct {
	ID                 string
	CompletedAt        sql.NullTime
	PausedAt           sql.NullTime
	PausedTotalSeconds int32
}

func (q *Queries) UpdateFocusSession(ctx context.Context, arg UpdateFocusSessionParams) (FocusSession, error) {
	row := q.db.QueryRowContext(ctx, updateFocusSession,
		arg.ID,
		arg.CompletedAt,
		arg.PausedAt,
		arg.PausedTotalSeconds,
	)
	var i FocusSession
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.TaskID,
		&i.Mode,
		&i.StartedAt,
		&i.CompletedAt,
		&i.PausedAt,
		&i.PausedTotalSeconds,
		&i.DurationSeconds,
	)
	return i, err
}

const updateNote = `-- name: UpdateNote :one
UPDATE notes
SET title = $2,
    content = $3,
    updated_at = $4
WHERE id = $1
RETURNING id, workspace_slug, title, content, updated_at
`

type UpdateNoteParams struct {
	ID        string
	Title     string
	Content   string
	UpdatedAt time.Time
}

func (q *Queries) UpdateNote(ctx context.Context, arg UpdateNoteParams) (Note, error) {
	row := q.db.QueryRowContext(ctx, updateNote,
		arg.ID,
		arg.Title,
		arg.Content,
		arg.UpdatedAt,
	)
	var i Note
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.Title,
		&i.Content,
		&i.UpdatedAt,
	)
	return i, err
}

const updateTask = `-- name: UpdateTask :one
UPDATE tasks
SET title = $2,
    description = $3,
    status = $4,
    board_group_id = $5,
    color = $6,
    due_at = $7,
    scheduled_start = $8,
    scheduled_end = $9,
    assignee_id = $10,
    label_ids = $11,
    attachments = $12,
    comments = $13,
    updated_at = $14
WHERE id = $1
RETURNING id, workspace_slug, board_group_id, title, description, status, color, due_at, scheduled_start, scheduled_end, assignee_id, label_ids, attachments, comments, created_at, updated_at
`

type UpdateTaskParams struct {
	ID             string
	Title          string
	Description    string
	Status         string
	BoardGroupID   string
	Color          string
	DueAt          sql.NullTime
	ScheduledStart sql.NullTime
	ScheduledEnd   sql.NullTime
	AssigneeID     sql.NullString
	LabelIds       json.RawMessage
	Attachments    json.RawMessage
	Comments       json.RawMessage
	UpdatedAt      time.Time
}

func (q *Queries) UpdateTask(ctx context.Context, arg UpdateTaskParams) (Task, error) {
	row := q.db.QueryRowContext(ctx, updateTask,
		arg.ID,
		arg.Title,
		arg.Description,
		arg.Status,
		arg.BoardGroupID,
		arg.Color,
		arg.DueAt,
		arg.ScheduledStart,
		arg.ScheduledEnd,
		arg.AssigneeID,
		arg.LabelIds,
		arg.Attachments,
		arg.Comments,
		arg.UpdatedAt,
	)
	var i Task
	err := row.Scan(
		&i.ID,
		&i.WorkspaceSlug,
		&i.BoardGroupID,
		&i.Title,
		&i.Description,
		&i.Status,
		&i.Color,
		&i.DueAt,
		&i.ScheduledStart,
		&i.ScheduledEnd,
		&i.AssigneeID,
		&i.LabelIds,
		&i.Attachments,
		&i.Comments,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
