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

package sqlc

import (
	"context"

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

const bulkDeleteEntityWikisByEntityId = `-- name: BulkDeleteEntityWikisByEntityId :many
DELETE FROM entity_wikis
WHERE entity_id = $1
RETURNING wiki_id
`

func (q *Queries) BulkDeleteEntityWikisByEntityId(ctx context.Context, entityID pgtype.UUID) ([]pgtype.UUID, error) {
	rows, err := q.db.Query(ctx, bulkDeleteEntityWikisByEntityId, entityID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []pgtype.UUID{}
	for rows.Next() {
		var wiki_id pgtype.UUID
		if err := rows.Scan(&wiki_id); err != nil {
			return nil, err
		}
		items = append(items, wiki_id)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const bulkDeleteEntityWikisByWikiID = `-- name: BulkDeleteEntityWikisByWikiID :exec
DELETE FROM entity_wikis
WHERE wiki_id = $1
`

func (q *Queries) BulkDeleteEntityWikisByWikiID(ctx context.Context, wikiID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, bulkDeleteEntityWikisByWikiID, wikiID)
	return err
}

const createEntityWikis = `-- name: CreateEntityWikis :exec
INSERT INTO entity_wikis (
    entity_id, wiki_id, project_id
)
SELECT $1, unnest($3::uuid[]), $2
ON CONFLICT DO NOTHING
`

type CreateEntityWikisParams struct {
	EntityID  pgtype.UUID   `json:"entity_id"`
	ProjectID pgtype.UUID   `json:"project_id"`
	WikiIds   []pgtype.UUID `json:"wiki_ids"`
}

func (q *Queries) CreateEntityWikis(ctx context.Context, arg CreateEntityWikisParams) error {
	_, err := q.db.Exec(ctx, createEntityWikis, arg.EntityID, arg.ProjectID, arg.WikiIds)
	return err
}

const createWiki = `-- name: CreateWiki :one
INSERT INTO wikis (
    id, title, slug, project_id
) VALUES (
    COALESCE($4::uuid, uuidv7()), $1, $2, $3
)
RETURNING id, project_id, title, slug, is_deleted, created_at, updated_at
`

type CreateWikiParams struct {
	Title     pgtype.Text `json:"title"`
	Slug      pgtype.Text `json:"slug"`
	ProjectID pgtype.UUID `json:"project_id"`
	ID        pgtype.UUID `json:"id"`
}

func (q *Queries) CreateWiki(ctx context.Context, arg CreateWikiParams) (Wiki, error) {
	row := q.db.QueryRow(ctx, createWiki,
		arg.Title,
		arg.Slug,
		arg.ProjectID,
		arg.ID,
	)
	var i Wiki
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Title,
		&i.Slug,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const createWikiContent = `-- name: CreateWikiContent :one
INSERT INTO wiki_content (
    id, wiki_id, title, content
) VALUES (
    COALESCE($4::uuid, uuidv7()), $1, $2, $3
)
RETURNING id, wiki_id, title, content, is_deleted, created_at
`

type CreateWikiContentParams struct {
	WikiID  pgtype.UUID `json:"wiki_id"`
	Title   string      `json:"title"`
	Content pgtype.Text `json:"content"`
	ID      pgtype.UUID `json:"id"`
}

func (q *Queries) CreateWikiContent(ctx context.Context, arg CreateWikiContentParams) (WikiContent, error) {
	row := q.db.QueryRow(ctx, createWikiContent,
		arg.WikiID,
		arg.Title,
		arg.Content,
		arg.ID,
	)
	var i WikiContent
	err := row.Scan(
		&i.ID,
		&i.WikiID,
		&i.Title,
		&i.Content,
		&i.IsDeleted,
		&i.CreatedAt,
	)
	return i, err
}

const deleteEntityWiki = `-- name: DeleteEntityWiki :exec
DELETE FROM entity_wikis
WHERE entity_id = $1 AND wiki_id = $2
`

type DeleteEntityWikiParams struct {
	EntityID pgtype.UUID `json:"entity_id"`
	WikiID   pgtype.UUID `json:"wiki_id"`
}

func (q *Queries) DeleteEntityWiki(ctx context.Context, arg DeleteEntityWikiParams) error {
	_, err := q.db.Exec(ctx, deleteEntityWiki, arg.EntityID, arg.WikiID)
	return err
}

const deleteEntityWikisByProjectID = `-- name: DeleteEntityWikisByProjectID :exec
DELETE FROM entity_wikis
WHERE project_id = $1
`

func (q *Queries) DeleteEntityWikisByProjectID(ctx context.Context, projectID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteEntityWikisByProjectID, projectID)
	return err
}

const deleteWiki = `-- name: DeleteWiki :exec
UPDATE wikis
SET 
    is_deleted = true
WHERE id = $1
`

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

const deleteWikisByIDs = `-- name: DeleteWikisByIDs :exec
UPDATE wikis
SET is_deleted = true
WHERE id = ANY($1::uuid[])
`

func (q *Queries) DeleteWikisByIDs(ctx context.Context, dollar_1 []pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteWikisByIDs, dollar_1)
	return err
}

const getWikiById = `-- name: GetWikiById :one
SELECT id, project_id, title, slug, is_deleted, created_at, updated_at
FROM wikis
WHERE id = $1 AND is_deleted = false
`

func (q *Queries) GetWikiById(ctx context.Context, id pgtype.UUID) (Wiki, error) {
	row := q.db.QueryRow(ctx, getWikiById, id)
	var i Wiki
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Title,
		&i.Slug,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getWikiBySlug = `-- name: GetWikiBySlug :one
SELECT id, project_id, title, slug, is_deleted, created_at, updated_at
FROM wikis
WHERE slug = $1 AND is_deleted = false
`

func (q *Queries) GetWikiBySlug(ctx context.Context, slug pgtype.Text) (Wiki, error) {
	row := q.db.QueryRow(ctx, getWikiBySlug, slug)
	var i Wiki
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Title,
		&i.Slug,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getWikiContentByIDs = `-- name: GetWikiContentByIDs :many
SELECT id, wiki_id, title, content, is_deleted, created_at
FROM wiki_content
WHERE id = ANY($1::uuid[]) AND is_deleted = false
`

func (q *Queries) GetWikiContentByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]WikiContent, error) {
	rows, err := q.db.Query(ctx, getWikiContentByIDs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []WikiContent{}
	for rows.Next() {
		var i WikiContent
		if err := rows.Scan(
			&i.ID,
			&i.WikiID,
			&i.Title,
			&i.Content,
			&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 getWikiContentById = `-- name: GetWikiContentById :one
SELECT id, wiki_id, title, content, is_deleted, created_at
FROM wiki_content
WHERE id = $1 AND is_deleted = false
`

func (q *Queries) GetWikiContentById(ctx context.Context, id pgtype.UUID) (WikiContent, error) {
	row := q.db.QueryRow(ctx, getWikiContentById, id)
	var i WikiContent
	err := row.Scan(
		&i.ID,
		&i.WikiID,
		&i.Title,
		&i.Content,
		&i.IsDeleted,
		&i.CreatedAt,
	)
	return i, err
}

const getWikiContentByWikiID = `-- name: GetWikiContentByWikiID :many
SELECT id, title, created_at
FROM wiki_content
WHERE wiki_id = $1 AND is_deleted = false
ORDER BY created_at DESC
`

type GetWikiContentByWikiIDRow struct {
	ID        pgtype.UUID        `json:"id"`
	Title     string             `json:"title"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
}

func (q *Queries) GetWikiContentByWikiID(ctx context.Context, wikiID pgtype.UUID) ([]GetWikiContentByWikiIDRow, error) {
	rows, err := q.db.Query(ctx, getWikiContentByWikiID, wikiID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetWikiContentByWikiIDRow{}
	for rows.Next() {
		var i GetWikiContentByWikiIDRow
		if err := rows.Scan(&i.ID, &i.Title, &i.CreatedAt); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getWikiContentByWikiIDs = `-- name: GetWikiContentByWikiIDs :many
SELECT id, wiki_id, title, created_at
FROM wiki_content
WHERE wiki_id = ANY($1::uuid[]) AND is_deleted = false
ORDER BY created_at DESC
`

type GetWikiContentByWikiIDsRow struct {
	ID        pgtype.UUID        `json:"id"`
	WikiID    pgtype.UUID        `json:"wiki_id"`
	Title     string             `json:"title"`
	CreatedAt pgtype.Timestamptz `json:"created_at"`
}

func (q *Queries) GetWikiContentByWikiIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]GetWikiContentByWikiIDsRow, error) {
	rows, err := q.db.Query(ctx, getWikiContentByWikiIDs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetWikiContentByWikiIDsRow{}
	for rows.Next() {
		var i GetWikiContentByWikiIDsRow
		if err := rows.Scan(
			&i.ID,
			&i.WikiID,
			&i.Title,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getWikiContentCount = `-- name: GetWikiContentCount :one
SELECT COUNT(*)
FROM wiki_content
WHERE wiki_id = $1
`

func (q *Queries) GetWikiContentCount(ctx context.Context, wikiID pgtype.UUID) (int64, error) {
	row := q.db.QueryRow(ctx, getWikiContentCount, wikiID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const getWikisByIDs = `-- name: GetWikisByIDs :many
SELECT id, project_id, title, slug, is_deleted, created_at, updated_at FROM wikis WHERE id = ANY($1::uuid[]) AND is_deleted = false
`

func (q *Queries) GetWikisByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]Wiki, error) {
	rows, err := q.db.Query(ctx, getWikisByIDs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Wiki{}
	for rows.Next() {
		var i Wiki
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.Title,
			&i.Slug,
			&i.IsDeleted,
			&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 getWikisByProjectId = `-- name: GetWikisByProjectId :many
SELECT id, project_id, title, slug, is_deleted, created_at, updated_at
FROM wikis
WHERE project_id = $1 AND is_deleted = false
`

func (q *Queries) GetWikisByProjectId(ctx context.Context, projectID pgtype.UUID) ([]Wiki, error) {
	rows, err := q.db.Query(ctx, getWikisByProjectId, projectID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Wiki{}
	for rows.Next() {
		var i Wiki
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.Title,
			&i.Slug,
			&i.IsDeleted,
			&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 getWikisBySlugs = `-- name: GetWikisBySlugs :many
SELECT id, project_id, title, slug, is_deleted, created_at, updated_at FROM wikis WHERE slug = ANY($1::text[]) AND is_deleted = false
`

func (q *Queries) GetWikisBySlugs(ctx context.Context, dollar_1 []string) ([]Wiki, error) {
	rows, err := q.db.Query(ctx, getWikisBySlugs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Wiki{}
	for rows.Next() {
		var i Wiki
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.Title,
			&i.Slug,
			&i.IsDeleted,
			&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 searchWikis = `-- name: SearchWikis :many
SELECT w.id, w.project_id, w.title, w.slug, w.is_deleted, w.created_at, w.updated_at
FROM wikis w
WHERE w.is_deleted = false
  AND ($1::uuid IS NULL OR w.project_id = $1::uuid)
  AND w.title ILIKE '%' || $2::text || '%'
  AND (
    $3::uuid IS NULL OR
    EXISTS (
        SELECT 1 
        FROM entity_wikis ew 
        WHERE ew.wiki_id = w.id 
          AND ew.entity_id = $3::uuid
    )
  )
  AND ($4::uuid IS NULL OR w.id < $4::uuid)

ORDER BY w.id DESC
LIMIT $5
`

type SearchWikisParams struct {
	ProjectID  pgtype.UUID `json:"project_id"`
	Title      string      `json:"title"`
	EntityID   pgtype.UUID `json:"entity_id"`
	CursorID   pgtype.UUID `json:"cursor_id"`
	LimitCount int32       `json:"limit_count"`
}

func (q *Queries) SearchWikis(ctx context.Context, arg SearchWikisParams) ([]Wiki, error) {
	rows, err := q.db.Query(ctx, searchWikis,
		arg.ProjectID,
		arg.Title,
		arg.EntityID,
		arg.CursorID,
		arg.LimitCount,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Wiki{}
	for rows.Next() {
		var i Wiki
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.Title,
			&i.Slug,
			&i.IsDeleted,
			&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 updateWiki = `-- name: UpdateWiki :one
UPDATE wikis
SET 
    title = COALESCE($1, title),
    slug = COALESCE($2, slug),
    project_id = COALESCE($3, project_id)
WHERE id = $4 AND is_deleted = false
RETURNING id, project_id, title, slug, is_deleted, created_at, updated_at
`

type UpdateWikiParams struct {
	Title     pgtype.Text `json:"title"`
	Slug      pgtype.Text `json:"slug"`
	ProjectID pgtype.UUID `json:"project_id"`
	ID        pgtype.UUID `json:"id"`
}

func (q *Queries) UpdateWiki(ctx context.Context, arg UpdateWikiParams) (Wiki, error) {
	row := q.db.QueryRow(ctx, updateWiki,
		arg.Title,
		arg.Slug,
		arg.ProjectID,
		arg.ID,
	)
	var i Wiki
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.Title,
		&i.Slug,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
