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

package sqlc

import (
	"context"
	"encoding/json"

	"github.com/jackc/pgx/v5/pgtype"
)

const createCommit = `-- name: CreateCommit :one
INSERT INTO commits (
    project_id, snapshot_json, snapshot_hash, user_id, edit_summary
) VALUES (
    $1, $2, $3, $4, $5
)
RETURNING id, project_id, snapshot_json, snapshot_hash, user_id, edit_summary, is_deleted, created_at
`

type CreateCommitParams struct {
	ProjectID    pgtype.UUID     `json:"project_id"`
	SnapshotJson json.RawMessage `json:"snapshot_json"`
	SnapshotHash pgtype.Text     `json:"snapshot_hash"`
	UserID       pgtype.UUID     `json:"user_id"`
	EditSummary  pgtype.Text     `json:"edit_summary"`
}

func (q *Queries) CreateCommit(ctx context.Context, arg CreateCommitParams) (Commit, error) {
	row := q.db.QueryRow(ctx, createCommit,
		arg.ProjectID,
		arg.SnapshotJson,
		arg.SnapshotHash,
		arg.UserID,
		arg.EditSummary,
	)
	var i Commit
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.SnapshotJson,
		&i.SnapshotHash,
		&i.UserID,
		&i.EditSummary,
		&i.IsDeleted,
		&i.CreatedAt,
	)
	return i, err
}

const deleteCommit = `-- name: DeleteCommit :exec
UPDATE commits
SET is_deleted = true
WHERE id = $1
`

func (q *Queries) DeleteCommit(ctx context.Context, id pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteCommit, id)
	return err
}

const getCommitById = `-- name: GetCommitById :one
SELECT id, project_id, snapshot_json, snapshot_hash, user_id, edit_summary, is_deleted, created_at
FROM commits
WHERE id = $1 AND is_deleted = false
`

func (q *Queries) GetCommitById(ctx context.Context, id pgtype.UUID) (Commit, error) {
	row := q.db.QueryRow(ctx, getCommitById, id)
	var i Commit
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.SnapshotJson,
		&i.SnapshotHash,
		&i.UserID,
		&i.EditSummary,
		&i.IsDeleted,
		&i.CreatedAt,
	)
	return i, err
}

const getCommitsByIDs = `-- name: GetCommitsByIDs :many
SELECT id, project_id, snapshot_json, snapshot_hash, user_id, edit_summary, is_deleted, created_at FROM commits WHERE id = ANY($1::uuid[]) AND is_deleted = false
`

func (q *Queries) GetCommitsByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]Commit, error) {
	rows, err := q.db.Query(ctx, getCommitsByIDs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Commit{}
	for rows.Next() {
		var i Commit
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.SnapshotJson,
			&i.SnapshotHash,
			&i.UserID,
			&i.EditSummary,
			&i.IsDeleted,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getCommitsByProjectID = `-- name: GetCommitsByProjectID :many
SELECT id, project_id, snapshot_json, snapshot_hash, user_id, edit_summary, is_deleted, created_at
FROM commits
WHERE project_id = $1 AND is_deleted = false
ORDER BY created_at DESC
`

func (q *Queries) GetCommitsByProjectID(ctx context.Context, projectID pgtype.UUID) ([]Commit, error) {
	rows, err := q.db.Query(ctx, getCommitsByProjectID, projectID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Commit{}
	for rows.Next() {
		var i Commit
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.SnapshotJson,
			&i.SnapshotHash,
			&i.UserID,
			&i.EditSummary,
			&i.IsDeleted,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const searchCommits = `-- name: SearchCommits :many
SELECT id, project_id, snapshot_json, snapshot_hash, user_id, edit_summary, is_deleted, created_at
FROM commits
WHERE is_deleted = false
  AND ($1::uuid IS NULL OR project_id = $1)
  AND ($2::uuid IS NULL OR user_id = $2)
  AND ($3::uuid IS NULL OR id < $3::uuid)
ORDER BY created_at DESC
LIMIT $4
`

type SearchCommitsParams struct {
	ProjectID pgtype.UUID `json:"project_id"`
	UserID    pgtype.UUID `json:"user_id"`
	CursorID  pgtype.UUID `json:"cursor_id"`
	Limit     int32       `json:"limit"`
}

func (q *Queries) SearchCommits(ctx context.Context, arg SearchCommitsParams) ([]Commit, error) {
	rows, err := q.db.Query(ctx, searchCommits,
		arg.ProjectID,
		arg.UserID,
		arg.CursorID,
		arg.Limit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Commit{}
	for rows.Next() {
		var i Commit
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.SnapshotJson,
			&i.SnapshotHash,
			&i.UserID,
			&i.EditSummary,
			&i.IsDeleted,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateCommitSnapshot = `-- name: UpdateCommitSnapshot :one
UPDATE commits
SET snapshot_json = $2
WHERE id = $1
RETURNING id, project_id, snapshot_json, snapshot_hash, user_id, edit_summary, is_deleted, created_at
`

type UpdateCommitSnapshotParams struct {
	ID           pgtype.UUID     `json:"id"`
	SnapshotJson json.RawMessage `json:"snapshot_json"`
}

func (q *Queries) UpdateCommitSnapshot(ctx context.Context, arg UpdateCommitSnapshotParams) (Commit, error) {
	row := q.db.QueryRow(ctx, updateCommitSnapshot, arg.ID, arg.SnapshotJson)
	var i Commit
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.SnapshotJson,
		&i.SnapshotHash,
		&i.UserID,
		&i.EditSummary,
		&i.IsDeleted,
		&i.CreatedAt,
	)
	return i, err
}
