// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: workspace_slots.sql

package db

import (
	"context"
)

const listWorkspaceSlotsByProject = `-- name: ListWorkspaceSlotsByProject :many
SELECT id, project_sync_setting_id, slot_index, state, path, created_at, updated_at
FROM workspace_slots
WHERE project_sync_setting_id = $1
ORDER BY slot_index
`

func (q *Queries) ListWorkspaceSlotsByProject(ctx context.Context, projectSyncSettingID int64) ([]WorkspaceSlot, error) {
	rows, err := q.db.Query(ctx, listWorkspaceSlotsByProject, projectSyncSettingID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []WorkspaceSlot
	for rows.Next() {
		var i WorkspaceSlot
		if err := rows.Scan(
			&i.ID,
			&i.ProjectSyncSettingID,
			&i.SlotIndex,
			&i.State,
			&i.Path,
			&i.CreatedAt,
			&i.UpdatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const reserveAvailableWorkspaceSlot = `-- name: ReserveAvailableWorkspaceSlot :one
UPDATE workspace_slots
SET state = 'in_use', updated_at = now()
WHERE id = (
    SELECT ws.id FROM workspace_slots ws
    WHERE ws.project_sync_setting_id = $1 AND ws.state = 'available'
    ORDER BY ws.slot_index
    LIMIT 1
    FOR UPDATE SKIP LOCKED
)
RETURNING id, project_sync_setting_id, slot_index, state, path, created_at, updated_at
`

func (q *Queries) ReserveAvailableWorkspaceSlot(ctx context.Context, projectSyncSettingID int64) (WorkspaceSlot, error) {
	row := q.db.QueryRow(ctx, reserveAvailableWorkspaceSlot, projectSyncSettingID)
	var i WorkspaceSlot
	err := row.Scan(
		&i.ID,
		&i.ProjectSyncSettingID,
		&i.SlotIndex,
		&i.State,
		&i.Path,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const updateWorkspaceSlotState = `-- name: UpdateWorkspaceSlotState :one
UPDATE workspace_slots
SET state = $2, updated_at = now()
WHERE id = $1
RETURNING id, project_sync_setting_id, slot_index, state, path, created_at, updated_at
`

type UpdateWorkspaceSlotStateParams struct {
	ID    int64  `json:"id"`
	State string `json:"state"`
}

func (q *Queries) UpdateWorkspaceSlotState(ctx context.Context, arg UpdateWorkspaceSlotStateParams) (WorkspaceSlot, error) {
	row := q.db.QueryRow(ctx, updateWorkspaceSlotState, arg.ID, arg.State)
	var i WorkspaceSlot
	err := row.Scan(
		&i.ID,
		&i.ProjectSyncSettingID,
		&i.SlotIndex,
		&i.State,
		&i.Path,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const upsertWorkspaceSlot = `-- name: UpsertWorkspaceSlot :one
INSERT INTO workspace_slots (project_sync_setting_id, slot_index, state, path)
VALUES ($1, $2, 'available', $3)
ON CONFLICT (project_sync_setting_id, slot_index)
DO UPDATE SET path = EXCLUDED.path, updated_at = now()
RETURNING id, project_sync_setting_id, slot_index, state, path, created_at, updated_at
`

type UpsertWorkspaceSlotParams struct {
	ProjectSyncSettingID int64  `json:"project_sync_setting_id"`
	SlotIndex            int32  `json:"slot_index"`
	Path                 string `json:"path"`
}

func (q *Queries) UpsertWorkspaceSlot(ctx context.Context, arg UpsertWorkspaceSlotParams) (WorkspaceSlot, error) {
	row := q.db.QueryRow(ctx, upsertWorkspaceSlot, arg.ProjectSyncSettingID, arg.SlotIndex, arg.Path)
	var i WorkspaceSlot
	err := row.Scan(
		&i.ID,
		&i.ProjectSyncSettingID,
		&i.SlotIndex,
		&i.State,
		&i.Path,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
