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

package sqlc

import (
	"context"
	"encoding/json"

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

const createBattleReplay = `-- name: CreateBattleReplay :one
INSERT INTO battle_replays (
    id, geometry_id, project_id, target_geometry_ids, detail
) VALUES (
    COALESCE($5::uuid, uuidv7()), $1, $2, $3, $4
)
RETURNING id, geometry_id, project_id, target_geometry_ids, detail, is_deleted, created_at, updated_at
`

type CreateBattleReplayParams struct {
	GeometryID        pgtype.UUID     `json:"geometry_id"`
	ProjectID         pgtype.UUID     `json:"project_id"`
	TargetGeometryIds json.RawMessage `json:"target_geometry_ids"`
	Detail            json.RawMessage `json:"detail"`
	ID                pgtype.UUID     `json:"id"`
}

func (q *Queries) CreateBattleReplay(ctx context.Context, arg CreateBattleReplayParams) (BattleReplay, error) {
	row := q.db.QueryRow(ctx, createBattleReplay,
		arg.GeometryID,
		arg.ProjectID,
		arg.TargetGeometryIds,
		arg.Detail,
		arg.ID,
	)
	var i BattleReplay
	err := row.Scan(
		&i.ID,
		&i.GeometryID,
		&i.ProjectID,
		&i.TargetGeometryIds,
		&i.Detail,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const deleteBattleReplay = `-- name: DeleteBattleReplay :exec
UPDATE battle_replays
SET is_deleted = true
WHERE id = $1
`

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

const deleteBattleReplaysByIDs = `-- name: DeleteBattleReplaysByIDs :exec
UPDATE battle_replays
SET is_deleted = true
WHERE id = ANY($1::uuid[])
`

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

const getBattleReplayById = `-- name: GetBattleReplayById :one
SELECT id, geometry_id, project_id, target_geometry_ids, detail, is_deleted, created_at, updated_at
FROM battle_replays
WHERE id = $1 AND is_deleted = false
`

func (q *Queries) GetBattleReplayById(ctx context.Context, id pgtype.UUID) (BattleReplay, error) {
	row := q.db.QueryRow(ctx, getBattleReplayById, id)
	var i BattleReplay
	err := row.Scan(
		&i.ID,
		&i.GeometryID,
		&i.ProjectID,
		&i.TargetGeometryIds,
		&i.Detail,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getBattleReplaysByGeometryIDs = `-- name: GetBattleReplaysByGeometryIDs :many
SELECT id, geometry_id, project_id, target_geometry_ids, detail, is_deleted, created_at, updated_at
FROM battle_replays
WHERE geometry_id = ANY($1::uuid[]) AND is_deleted = false
`

func (q *Queries) GetBattleReplaysByGeometryIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]BattleReplay, error) {
	rows, err := q.db.Query(ctx, getBattleReplaysByGeometryIDs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []BattleReplay{}
	for rows.Next() {
		var i BattleReplay
		if err := rows.Scan(
			&i.ID,
			&i.GeometryID,
			&i.ProjectID,
			&i.TargetGeometryIds,
			&i.Detail,
			&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 getBattleReplaysByGeometryId = `-- name: GetBattleReplaysByGeometryId :many
SELECT id, geometry_id, project_id, target_geometry_ids, detail, is_deleted, created_at, updated_at
FROM battle_replays
WHERE geometry_id = $1 AND is_deleted = false
`

func (q *Queries) GetBattleReplaysByGeometryId(ctx context.Context, geometryID pgtype.UUID) ([]BattleReplay, error) {
	rows, err := q.db.Query(ctx, getBattleReplaysByGeometryId, geometryID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []BattleReplay{}
	for rows.Next() {
		var i BattleReplay
		if err := rows.Scan(
			&i.ID,
			&i.GeometryID,
			&i.ProjectID,
			&i.TargetGeometryIds,
			&i.Detail,
			&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 getBattleReplaysByIDs = `-- name: GetBattleReplaysByIDs :many
SELECT id, geometry_id, project_id, target_geometry_ids, detail, is_deleted, created_at, updated_at FROM battle_replays WHERE id = ANY($1::uuid[]) AND is_deleted = false
`

func (q *Queries) GetBattleReplaysByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]BattleReplay, error) {
	rows, err := q.db.Query(ctx, getBattleReplaysByIDs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []BattleReplay{}
	for rows.Next() {
		var i BattleReplay
		if err := rows.Scan(
			&i.ID,
			&i.GeometryID,
			&i.ProjectID,
			&i.TargetGeometryIds,
			&i.Detail,
			&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 getBattleReplaysByProjectId = `-- name: GetBattleReplaysByProjectId :many
SELECT id, geometry_id, project_id, target_geometry_ids, detail, is_deleted, created_at, updated_at
FROM battle_replays
WHERE project_id = $1 AND is_deleted = false
`

func (q *Queries) GetBattleReplaysByProjectId(ctx context.Context, projectID pgtype.UUID) ([]BattleReplay, error) {
	rows, err := q.db.Query(ctx, getBattleReplaysByProjectId, projectID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []BattleReplay{}
	for rows.Next() {
		var i BattleReplay
		if err := rows.Scan(
			&i.ID,
			&i.GeometryID,
			&i.ProjectID,
			&i.TargetGeometryIds,
			&i.Detail,
			&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 updateBattleReplay = `-- name: UpdateBattleReplay :one
UPDATE battle_replays
SET
    geometry_id = COALESCE($1, geometry_id),
    target_geometry_ids = COALESCE($2, target_geometry_ids),
    detail = COALESCE($3, detail)
WHERE id = $4 AND is_deleted = false
RETURNING id, geometry_id, project_id, target_geometry_ids, detail, is_deleted, created_at, updated_at
`

type UpdateBattleReplayParams struct {
	GeometryID        pgtype.UUID `json:"geometry_id"`
	TargetGeometryIds []byte      `json:"target_geometry_ids"`
	Detail            []byte      `json:"detail"`
	ID                pgtype.UUID `json:"id"`
}

func (q *Queries) UpdateBattleReplay(ctx context.Context, arg UpdateBattleReplayParams) (BattleReplay, error) {
	row := q.db.QueryRow(ctx, updateBattleReplay,
		arg.GeometryID,
		arg.TargetGeometryIds,
		arg.Detail,
		arg.ID,
	)
	var i BattleReplay
	err := row.Scan(
		&i.ID,
		&i.GeometryID,
		&i.ProjectID,
		&i.TargetGeometryIds,
		&i.Detail,
		&i.IsDeleted,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
