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

package sqlc

import (
	"context"

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

const bulkDeleteVerificationMediaByMediaId = `-- name: BulkDeleteVerificationMediaByMediaId :many
DELETE FROM verification_medias
WHERE media_id = $1
RETURNING verification_id
`

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

const countUserVerifications = `-- name: CountUserVerifications :one
SELECT count(*) 
FROM user_verifications uv
WHERE 
    uv.is_deleted = false
    AND ($1::uuid[] IS NULL OR uv.user_id = ANY($1::uuid[]))
    AND (
        $2::smallint[] IS NULL 
        OR uv.verify_type = ANY($2::smallint[])
    )
    AND (
        $3::smallint[] IS NULL 
        OR uv.status = ANY($3::smallint[])
    )
    AND ($4::uuid IS NULL OR uv.reviewed_by = $4::uuid)
    AND ($5::timestamptz IS NULL OR uv.created_at >= $5::timestamptz)
    AND ($6::timestamptz IS NULL OR uv.created_at <= $6::timestamptz)
    AND (
        $7::text IS NULL OR 
        uv.id::text ILIKE '%' || $7::text || '%' OR
        uv.content::text ILIKE '%' || $7::text || '%'
    )
`

type CountUserVerificationsParams struct {
	UserIds     []pgtype.UUID      `json:"user_ids"`
	VerifyTypes []int16            `json:"verify_types"`
	Statuses    []int16            `json:"statuses"`
	ReviewedBy  pgtype.UUID        `json:"reviewed_by"`
	CreatedFrom pgtype.Timestamptz `json:"created_from"`
	CreatedTo   pgtype.Timestamptz `json:"created_to"`
	SearchText  pgtype.Text        `json:"search_text"`
}

func (q *Queries) CountUserVerifications(ctx context.Context, arg CountUserVerificationsParams) (int64, error) {
	row := q.db.QueryRow(ctx, countUserVerifications,
		arg.UserIds,
		arg.VerifyTypes,
		arg.Statuses,
		arg.ReviewedBy,
		arg.CreatedFrom,
		arg.CreatedTo,
		arg.SearchText,
	)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const createUserVerification = `-- name: CreateUserVerification :one
WITH inserted_uv AS (
    INSERT INTO user_verifications (
        user_id, verify_type, content
    ) VALUES (
        $1, $2, $3
    )
    RETURNING id, user_id, verify_type, content, is_deleted, status, reviewed_by, review_note, reviewed_at, created_at
)
SELECT 
    i.id, 
    i.verify_type, 
    i.content,
    i.is_deleted, 
    i.status, 
    i.review_note,
    i.reviewed_at, 
    i.created_at,
    json_build_object(
        'id', u.id,
        'email', u.email,
        'display_name', up.display_name,
        'full_name', up.full_name,
        'avatar_url', up.avatar_url
    )::json AS user,
    NULL::json AS reviewer,
    '[]'::json AS medias
FROM inserted_uv i
JOIN users u ON i.user_id = u.id
LEFT JOIN user_profiles up ON u.id = up.user_id
`

type CreateUserVerificationParams struct {
	UserID     pgtype.UUID `json:"user_id"`
	VerifyType int16       `json:"verify_type"`
	Content    pgtype.Text `json:"content"`
}

type CreateUserVerificationRow struct {
	ID         pgtype.UUID        `json:"id"`
	VerifyType int16              `json:"verify_type"`
	Content    pgtype.Text        `json:"content"`
	IsDeleted  bool               `json:"is_deleted"`
	Status     int16              `json:"status"`
	ReviewNote pgtype.Text        `json:"review_note"`
	ReviewedAt pgtype.Timestamptz `json:"reviewed_at"`
	CreatedAt  pgtype.Timestamptz `json:"created_at"`
	User       []byte             `json:"user"`
	Reviewer   []byte             `json:"reviewer"`
	Medias     []byte             `json:"medias"`
}

func (q *Queries) CreateUserVerification(ctx context.Context, arg CreateUserVerificationParams) (CreateUserVerificationRow, error) {
	row := q.db.QueryRow(ctx, createUserVerification, arg.UserID, arg.VerifyType, arg.Content)
	var i CreateUserVerificationRow
	err := row.Scan(
		&i.ID,
		&i.VerifyType,
		&i.Content,
		&i.IsDeleted,
		&i.Status,
		&i.ReviewNote,
		&i.ReviewedAt,
		&i.CreatedAt,
		&i.User,
		&i.Reviewer,
		&i.Medias,
	)
	return i, err
}

const createVerificationMedia = `-- name: CreateVerificationMedia :exec
INSERT INTO verification_medias (
    verification_id, media_id
)
SELECT $1, unnest($2::uuid[])
ON CONFLICT DO NOTHING
`

type CreateVerificationMediaParams struct {
	VerificationID pgtype.UUID   `json:"verification_id"`
	Column2        []pgtype.UUID `json:"column_2"`
}

func (q *Queries) CreateVerificationMedia(ctx context.Context, arg CreateVerificationMediaParams) error {
	_, err := q.db.Exec(ctx, createVerificationMedia, arg.VerificationID, arg.Column2)
	return err
}

const deleteUserVerification = `-- name: DeleteUserVerification :exec
UPDATE user_verifications
SET is_deleted = true
WHERE id = $1
`

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

const deleteVerificationMedia = `-- name: DeleteVerificationMedia :exec
DELETE FROM verification_medias
WHERE verification_id = $1 AND media_id = $2
`

type DeleteVerificationMediaParams struct {
	VerificationID pgtype.UUID `json:"verification_id"`
	MediaID        pgtype.UUID `json:"media_id"`
}

func (q *Queries) DeleteVerificationMedia(ctx context.Context, arg DeleteVerificationMediaParams) error {
	_, err := q.db.Exec(ctx, deleteVerificationMedia, arg.VerificationID, arg.MediaID)
	return err
}

const getUserVerificationByID = `-- name: GetUserVerificationByID :one
SELECT 
    uv.id, 
    uv.verify_type, 
    uv.content,
    uv.is_deleted, 
    uv.status, 
    uv.review_note,
    uv.reviewed_at, 
    uv.created_at,
    json_build_object(
        'id', u.id,
        'email', u.email,
        'display_name', up.display_name,
        'full_name', up.full_name,
        'avatar_url', up.avatar_url
    )::json AS user,
    CASE WHEN uv.reviewed_by IS NOT NULL THEN
        json_build_object(
            'id', ru.id,
            'email', ru.email,
            'display_name', rup.display_name,
            'full_name', rup.full_name,
            'avatar_url', rup.avatar_url
        )::json
    ELSE NULL::json END AS reviewer,
    (
        SELECT COALESCE(
            json_agg(
                json_build_object(
                    'id', m.id,
                    'storage_key', m.storage_key,
                    'original_name', m.original_name,
                    'mime_type', m.mime_type,
                    'size', m.size,
                    'file_metadata', m.file_metadata,
                    'created_at', m.created_at
                )
            ),
            '[]'
        )::json
        FROM verification_medias vm
        JOIN medias m ON vm.media_id = m.id
        WHERE vm.verification_id = uv.id
    ) AS medias
FROM user_verifications uv
JOIN users u ON uv.user_id = u.id
LEFT JOIN user_profiles up ON u.id = up.user_id
LEFT JOIN users ru ON uv.reviewed_by = ru.id
LEFT JOIN user_profiles rup ON ru.id = rup.user_id
WHERE uv.id = $1 AND uv.is_deleted = false
`

type GetUserVerificationByIDRow struct {
	ID         pgtype.UUID        `json:"id"`
	VerifyType int16              `json:"verify_type"`
	Content    pgtype.Text        `json:"content"`
	IsDeleted  bool               `json:"is_deleted"`
	Status     int16              `json:"status"`
	ReviewNote pgtype.Text        `json:"review_note"`
	ReviewedAt pgtype.Timestamptz `json:"reviewed_at"`
	CreatedAt  pgtype.Timestamptz `json:"created_at"`
	User       []byte             `json:"user"`
	Reviewer   []byte             `json:"reviewer"`
	Medias     []byte             `json:"medias"`
}

func (q *Queries) GetUserVerificationByID(ctx context.Context, id pgtype.UUID) (GetUserVerificationByIDRow, error) {
	row := q.db.QueryRow(ctx, getUserVerificationByID, id)
	var i GetUserVerificationByIDRow
	err := row.Scan(
		&i.ID,
		&i.VerifyType,
		&i.Content,
		&i.IsDeleted,
		&i.Status,
		&i.ReviewNote,
		&i.ReviewedAt,
		&i.CreatedAt,
		&i.User,
		&i.Reviewer,
		&i.Medias,
	)
	return i, err
}

const getUserVerifications = `-- name: GetUserVerifications :many
SELECT 
    uv.id, 
    uv.verify_type, 
    uv.content,
    uv.is_deleted, 
    uv.status, 
    uv.review_note,
    uv.reviewed_at, 
    uv.created_at,
    json_build_object(
        'id', u.id,
        'email', u.email,
        'display_name', up.display_name,
        'full_name', up.full_name,
        'avatar_url', up.avatar_url
    )::json AS user,
    CASE WHEN uv.reviewed_by IS NOT NULL THEN
        json_build_object(
            'id', ru.id,
            'email', ru.email,
            'display_name', rup.display_name,
            'full_name', rup.full_name,
            'avatar_url', rup.avatar_url
        )::json
    ELSE NULL::json END AS reviewer,
    (
        SELECT COALESCE(
            json_agg(
                json_build_object(
                    'id', m.id,
                    'storage_key', m.storage_key,
                    'original_name', m.original_name,
                    'mime_type', m.mime_type,
                    'size', m.size,
                    'file_metadata', m.file_metadata,
                    'created_at', m.created_at
                )
            ),
            '[]'
        )::json
        FROM verification_medias vm
        JOIN medias m ON vm.media_id = m.id
        WHERE vm.verification_id = uv.id
    ) AS medias
FROM user_verifications uv
JOIN users u ON uv.user_id = u.id
LEFT JOIN user_profiles up ON u.id = up.user_id
LEFT JOIN users ru ON uv.reviewed_by = ru.id
LEFT JOIN user_profiles rup ON ru.id = rup.user_id
WHERE uv.user_id = $1 AND uv.is_deleted = false
ORDER BY uv.created_at DESC
`

type GetUserVerificationsRow struct {
	ID         pgtype.UUID        `json:"id"`
	VerifyType int16              `json:"verify_type"`
	Content    pgtype.Text        `json:"content"`
	IsDeleted  bool               `json:"is_deleted"`
	Status     int16              `json:"status"`
	ReviewNote pgtype.Text        `json:"review_note"`
	ReviewedAt pgtype.Timestamptz `json:"reviewed_at"`
	CreatedAt  pgtype.Timestamptz `json:"created_at"`
	User       []byte             `json:"user"`
	Reviewer   []byte             `json:"reviewer"`
	Medias     []byte             `json:"medias"`
}

func (q *Queries) GetUserVerifications(ctx context.Context, userID pgtype.UUID) ([]GetUserVerificationsRow, error) {
	rows, err := q.db.Query(ctx, getUserVerifications, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetUserVerificationsRow{}
	for rows.Next() {
		var i GetUserVerificationsRow
		if err := rows.Scan(
			&i.ID,
			&i.VerifyType,
			&i.Content,
			&i.IsDeleted,
			&i.Status,
			&i.ReviewNote,
			&i.ReviewedAt,
			&i.CreatedAt,
			&i.User,
			&i.Reviewer,
			&i.Medias,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const getUserVerificationsByIDs = `-- name: GetUserVerificationsByIDs :many
SELECT 
    uv.id, uv.verify_type, uv.content,
    uv.is_deleted, uv.status, uv.review_note,
    uv.reviewed_at, uv.created_at,
    json_build_object(
        'id', u.id,
        'email', u.email,
        'display_name', up.display_name,
        'full_name', up.full_name,
        'avatar_url', up.avatar_url
    )::json AS user,
    CASE WHEN uv.reviewed_by IS NOT NULL THEN
        json_build_object(
            'id', ru.id,
            'email', ru.email,
            'display_name', rup.display_name,
            'full_name', rup.full_name,
            'avatar_url', rup.avatar_url
        )::json
    ELSE NULL::json END AS reviewer,
    (
        SELECT COALESCE(
            json_agg(
                json_build_object(
                    'id', m.id,
                    'storage_key', m.storage_key,
                    'original_name', m.original_name,
                    'mime_type', m.mime_type,
                    'size', m.size,
                    'file_metadata', m.file_metadata,
                    'created_at', m.created_at
                )
            ),
            '[]'
        )::json
        FROM verification_medias vm
        JOIN medias m ON vm.media_id = m.id
        WHERE vm.verification_id = uv.id
    ) AS medias
FROM user_verifications uv
JOIN users u ON uv.user_id = u.id
LEFT JOIN user_profiles up ON u.id = up.user_id
LEFT JOIN users ru ON uv.reviewed_by = ru.id
LEFT JOIN user_profiles rup ON ru.id = rup.user_id
WHERE uv.id = ANY($1::uuid[]) 
    AND uv.is_deleted = false
`

type GetUserVerificationsByIDsRow struct {
	ID         pgtype.UUID        `json:"id"`
	VerifyType int16              `json:"verify_type"`
	Content    pgtype.Text        `json:"content"`
	IsDeleted  bool               `json:"is_deleted"`
	Status     int16              `json:"status"`
	ReviewNote pgtype.Text        `json:"review_note"`
	ReviewedAt pgtype.Timestamptz `json:"reviewed_at"`
	CreatedAt  pgtype.Timestamptz `json:"created_at"`
	User       []byte             `json:"user"`
	Reviewer   []byte             `json:"reviewer"`
	Medias     []byte             `json:"medias"`
}

func (q *Queries) GetUserVerificationsByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]GetUserVerificationsByIDsRow, error) {
	rows, err := q.db.Query(ctx, getUserVerificationsByIDs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetUserVerificationsByIDsRow{}
	for rows.Next() {
		var i GetUserVerificationsByIDsRow
		if err := rows.Scan(
			&i.ID,
			&i.VerifyType,
			&i.Content,
			&i.IsDeleted,
			&i.Status,
			&i.ReviewNote,
			&i.ReviewedAt,
			&i.CreatedAt,
			&i.User,
			&i.Reviewer,
			&i.Medias,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const searchUserVerifications = `-- name: SearchUserVerifications :many
SELECT 
    uv.id, 
    uv.verify_type, 
    uv.content,
    uv.is_deleted, 
    uv.status, 
    uv.review_note, 
    uv.reviewed_at, 
    uv.created_at,
    json_build_object(
        'id', u.id,
        'email', u.email,
        'display_name', up.display_name,
        'full_name', up.full_name,
        'avatar_url', up.avatar_url
    )::json AS user,
    CASE WHEN uv.reviewed_by IS NOT NULL THEN
        json_build_object(
            'id', ru.id,
            'email', ru.email,
            'display_name', rup.display_name,
            'full_name', rup.full_name,
            'avatar_url', rup.avatar_url
        )::json
    ELSE NULL::json END AS reviewer,
    (
        SELECT COALESCE(
            json_agg(
                json_build_object(
                    'id', m.id,
                    'storage_key', m.storage_key,
                    'original_name', m.original_name,
                    'mime_type', m.mime_type,
                    'size', m.size,
                    'file_metadata', m.file_metadata,
                    'created_at', m.created_at
                )
            ),
            '[]'
        )::json
        FROM verification_medias vm
        JOIN medias m ON vm.media_id = m.id
        WHERE vm.verification_id = uv.id
    ) AS medias
FROM user_verifications uv
JOIN users u ON uv.user_id = u.id
LEFT JOIN user_profiles up ON u.id = up.user_id
LEFT JOIN users ru ON uv.reviewed_by = ru.id
LEFT JOIN user_profiles rup ON ru.id = rup.user_id
WHERE 
    uv.is_deleted = false
    AND ($1::uuid[] IS NULL OR uv.user_id = ANY($1::uuid[]))
    AND (
        $2::smallint[] IS NULL 
        OR uv.verify_type = ANY($2::smallint[])
    )
    AND (
        $3::smallint[] IS NULL 
        OR uv.status = ANY($3::smallint[])
    )
    AND ($4::uuid IS NULL OR uv.reviewed_by = $4::uuid)
    AND ($5::timestamptz IS NULL OR uv.created_at >= $5::timestamptz)
    AND ($6::timestamptz IS NULL OR uv.created_at <= $6::timestamptz)
    AND (
        $7::text IS NULL OR 
        uv.id::text ILIKE '%' || $7::text || '%' OR
        uv.content::text ILIKE '%' || $7::text || '%'
    )
ORDER BY
    CASE WHEN $8 = 'created_at' AND $9 = 'asc' THEN uv.created_at END ASC,
    CASE WHEN $8 = 'created_at' AND $9 = 'desc' THEN uv.created_at END DESC,
    CASE WHEN $8 = 'reviewed_at' AND $9 = 'asc' THEN uv.reviewed_at END ASC,
    CASE WHEN $8 = 'reviewed_at' AND $9 = 'desc' THEN uv.reviewed_at END DESC,
    CASE WHEN $8 = 'status' AND $9 = 'asc' THEN uv.status END ASC,
    CASE WHEN $8 = 'status' AND $9 = 'desc' THEN uv.status END DESC,
    CASE WHEN $8 IS NULL THEN uv.created_at END DESC
LIMIT $11
OFFSET $10
`

type SearchUserVerificationsParams struct {
	UserIds     []pgtype.UUID      `json:"user_ids"`
	VerifyTypes []int16            `json:"verify_types"`
	Statuses    []int16            `json:"statuses"`
	ReviewedBy  pgtype.UUID        `json:"reviewed_by"`
	CreatedFrom pgtype.Timestamptz `json:"created_from"`
	CreatedTo   pgtype.Timestamptz `json:"created_to"`
	SearchText  pgtype.Text        `json:"search_text"`
	Sort        interface{}        `json:"sort"`
	Order       interface{}        `json:"order"`
	Offset      int32              `json:"offset"`
	Limit       int32              `json:"limit"`
}

type SearchUserVerificationsRow struct {
	ID         pgtype.UUID        `json:"id"`
	VerifyType int16              `json:"verify_type"`
	Content    pgtype.Text        `json:"content"`
	IsDeleted  bool               `json:"is_deleted"`
	Status     int16              `json:"status"`
	ReviewNote pgtype.Text        `json:"review_note"`
	ReviewedAt pgtype.Timestamptz `json:"reviewed_at"`
	CreatedAt  pgtype.Timestamptz `json:"created_at"`
	User       []byte             `json:"user"`
	Reviewer   []byte             `json:"reviewer"`
	Medias     []byte             `json:"medias"`
}

func (q *Queries) SearchUserVerifications(ctx context.Context, arg SearchUserVerificationsParams) ([]SearchUserVerificationsRow, error) {
	rows, err := q.db.Query(ctx, searchUserVerifications,
		arg.UserIds,
		arg.VerifyTypes,
		arg.Statuses,
		arg.ReviewedBy,
		arg.CreatedFrom,
		arg.CreatedTo,
		arg.SearchText,
		arg.Sort,
		arg.Order,
		arg.Offset,
		arg.Limit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []SearchUserVerificationsRow{}
	for rows.Next() {
		var i SearchUserVerificationsRow
		if err := rows.Scan(
			&i.ID,
			&i.VerifyType,
			&i.Content,
			&i.IsDeleted,
			&i.Status,
			&i.ReviewNote,
			&i.ReviewedAt,
			&i.CreatedAt,
			&i.User,
			&i.Reviewer,
			&i.Medias,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateUserVerificationStatus = `-- name: UpdateUserVerificationStatus :exec
UPDATE user_verifications
SET 
    status = $2,
    review_note = $3,
    reviewed_by = $4,
    reviewed_at = now()
WHERE id = $1 AND is_deleted = false
`

type UpdateUserVerificationStatusParams struct {
	ID         pgtype.UUID `json:"id"`
	Status     int16       `json:"status"`
	ReviewNote pgtype.Text `json:"review_note"`
	ReviewedBy pgtype.UUID `json:"reviewed_by"`
}

func (q *Queries) UpdateUserVerificationStatus(ctx context.Context, arg UpdateUserVerificationStatusParams) error {
	_, err := q.db.Exec(ctx, updateUserVerificationStatus,
		arg.ID,
		arg.Status,
		arg.ReviewNote,
		arg.ReviewedBy,
	)
	return err
}
