// 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 createUserVerification = `-- name: CreateUserVerification :one
INSERT INTO user_verifications (
    user_id, verify_type
) VALUES (
    $1, $2
)
RETURNING id, user_id, verify_type, is_deleted, status, reviewed_by, reviewed_at, created_at
`

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

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

const createVerificationMedia = `-- name: CreateVerificationMedia :exec
INSERT INTO verification_medias (
    verification_id, media_id
) VALUES (
    $1, $2
)
`

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

func (q *Queries) CreateVerificationMedia(ctx context.Context, arg CreateVerificationMediaParams) error {
	_, err := q.db.Exec(ctx, createVerificationMedia, arg.VerificationID, arg.MediaID)
	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.user_id, 
    uv.verify_type, 
    uv.is_deleted, 
    uv.status, 
    uv.reviewed_by, 
    uv.reviewed_at, 
    uv.created_at,
    (
        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
WHERE uv.id = $1 AND uv.is_deleted = false
`

type GetUserVerificationByIDRow struct {
	ID         pgtype.UUID        `json:"id"`
	UserID     pgtype.UUID        `json:"user_id"`
	VerifyType int16              `json:"verify_type"`
	IsDeleted  bool               `json:"is_deleted"`
	Status     int16              `json:"status"`
	ReviewedBy pgtype.UUID        `json:"reviewed_by"`
	ReviewedAt pgtype.Timestamptz `json:"reviewed_at"`
	CreatedAt  pgtype.Timestamptz `json:"created_at"`
	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.UserID,
		&i.VerifyType,
		&i.IsDeleted,
		&i.Status,
		&i.ReviewedBy,
		&i.ReviewedAt,
		&i.CreatedAt,
		&i.Medias,
	)
	return i, err
}

const getUserVerifications = `-- name: GetUserVerifications :many
SELECT 
    uv.id, 
    uv.user_id, 
    uv.verify_type, 
    uv.is_deleted, 
    uv.status, 
    uv.reviewed_by, 
    uv.reviewed_at, 
    uv.created_at,
    (
        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
WHERE uv.user_id = $1 AND uv.is_deleted = false
ORDER BY uv.created_at DESC
`

type GetUserVerificationsRow struct {
	ID         pgtype.UUID        `json:"id"`
	UserID     pgtype.UUID        `json:"user_id"`
	VerifyType int16              `json:"verify_type"`
	IsDeleted  bool               `json:"is_deleted"`
	Status     int16              `json:"status"`
	ReviewedBy pgtype.UUID        `json:"reviewed_by"`
	ReviewedAt pgtype.Timestamptz `json:"reviewed_at"`
	CreatedAt  pgtype.Timestamptz `json:"created_at"`
	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.UserID,
			&i.VerifyType,
			&i.IsDeleted,
			&i.Status,
			&i.ReviewedBy,
			&i.ReviewedAt,
			&i.CreatedAt,
			&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,
    reviewed_by = $3,
    reviewed_at = now()
WHERE id = $1 AND is_deleted = false
`

type UpdateUserVerificationStatusParams struct {
	ID         pgtype.UUID `json:"id"`
	Status     int16       `json:"status"`
	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.ReviewedBy)
	return err
}
