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

package sqlc

import (
	"context"

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

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

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

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

const createSubmission = `-- name: CreateSubmission :one
INSERT INTO submissions (
    project_id, commit_id, user_id, status, content
) VALUES (
    $1, $2, $3, $4, $5
)
RETURNING 
    id, project_id, commit_id, user_id, created_at, status, reviewed_by, reviewed_at, review_note, content, is_deleted,
    (SELECT title FROM projects WHERE id = submissions.project_id) AS project_title,
    (SELECT description FROM projects WHERE id = submissions.project_id) AS project_description,
    (
        SELECT json_build_object(
            'id', u.id,
            'email', u.email,
            'display_name', up.display_name,
            'full_name', up.full_name,
            'avatar_url', up.avatar_url
        )
        FROM users u 
        LEFT JOIN user_profiles up ON u.id = up.user_id
        WHERE u.id = submissions.user_id
    )::json AS user,
    (
        SELECT json_build_object(
            'id', ru.id,
            'email', ru.email,
            'display_name', rup.display_name,
            'full_name', rup.full_name,
            'avatar_url', rup.avatar_url
        )
        FROM users ru 
        LEFT JOIN user_profiles rup ON ru.id = rup.user_id
        WHERE ru.id = submissions.reviewed_by
    )::json AS reviewer
`

type CreateSubmissionParams struct {
	ProjectID pgtype.UUID `json:"project_id"`
	CommitID  pgtype.UUID `json:"commit_id"`
	UserID    pgtype.UUID `json:"user_id"`
	Status    int16       `json:"status"`
	Content   pgtype.Text `json:"content"`
}

type CreateSubmissionRow struct {
	ID                 pgtype.UUID        `json:"id"`
	ProjectID          pgtype.UUID        `json:"project_id"`
	CommitID           pgtype.UUID        `json:"commit_id"`
	UserID             pgtype.UUID        `json:"user_id"`
	CreatedAt          pgtype.Timestamptz `json:"created_at"`
	Status             int16              `json:"status"`
	ReviewedBy         pgtype.UUID        `json:"reviewed_by"`
	ReviewedAt         pgtype.Timestamptz `json:"reviewed_at"`
	ReviewNote         pgtype.Text        `json:"review_note"`
	Content            pgtype.Text        `json:"content"`
	IsDeleted          bool               `json:"is_deleted"`
	ProjectTitle       string             `json:"project_title"`
	ProjectDescription pgtype.Text        `json:"project_description"`
	User               []byte             `json:"user"`
	Reviewer           []byte             `json:"reviewer"`
}

func (q *Queries) CreateSubmission(ctx context.Context, arg CreateSubmissionParams) (CreateSubmissionRow, error) {
	row := q.db.QueryRow(ctx, createSubmission,
		arg.ProjectID,
		arg.CommitID,
		arg.UserID,
		arg.Status,
		arg.Content,
	)
	var i CreateSubmissionRow
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.CommitID,
		&i.UserID,
		&i.CreatedAt,
		&i.Status,
		&i.ReviewedBy,
		&i.ReviewedAt,
		&i.ReviewNote,
		&i.Content,
		&i.IsDeleted,
		&i.ProjectTitle,
		&i.ProjectDescription,
		&i.User,
		&i.Reviewer,
	)
	return i, err
}

const deleteSubmission = `-- name: DeleteSubmission :exec
UPDATE submissions
SET is_deleted = true
WHERE id = $1
`

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

const getSubmissionById = `-- name: GetSubmissionById :one
SELECT 
    s.id, s.project_id, s.commit_id, s.user_id, s.created_at, s.status, s.reviewed_by, s.reviewed_at, s.review_note, s.content, s.is_deleted,
    p.title AS project_title, p.description AS project_description,
    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 s.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
FROM submissions s
JOIN projects p ON s.project_id = p.id
JOIN users u ON s.user_id = u.id
LEFT JOIN user_profiles up ON u.id = up.user_id
LEFT JOIN users ru ON s.reviewed_by = ru.id
LEFT JOIN user_profiles rup ON ru.id = rup.user_id
WHERE s.id = $1 AND s.is_deleted = false
`

type GetSubmissionByIdRow struct {
	ID                 pgtype.UUID        `json:"id"`
	ProjectID          pgtype.UUID        `json:"project_id"`
	CommitID           pgtype.UUID        `json:"commit_id"`
	UserID             pgtype.UUID        `json:"user_id"`
	CreatedAt          pgtype.Timestamptz `json:"created_at"`
	Status             int16              `json:"status"`
	ReviewedBy         pgtype.UUID        `json:"reviewed_by"`
	ReviewedAt         pgtype.Timestamptz `json:"reviewed_at"`
	ReviewNote         pgtype.Text        `json:"review_note"`
	Content            pgtype.Text        `json:"content"`
	IsDeleted          bool               `json:"is_deleted"`
	ProjectTitle       string             `json:"project_title"`
	ProjectDescription pgtype.Text        `json:"project_description"`
	User               []byte             `json:"user"`
	Reviewer           []byte             `json:"reviewer"`
}

func (q *Queries) GetSubmissionById(ctx context.Context, id pgtype.UUID) (GetSubmissionByIdRow, error) {
	row := q.db.QueryRow(ctx, getSubmissionById, id)
	var i GetSubmissionByIdRow
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.CommitID,
		&i.UserID,
		&i.CreatedAt,
		&i.Status,
		&i.ReviewedBy,
		&i.ReviewedAt,
		&i.ReviewNote,
		&i.Content,
		&i.IsDeleted,
		&i.ProjectTitle,
		&i.ProjectDescription,
		&i.User,
		&i.Reviewer,
	)
	return i, err
}

const getSubmissionsByIDs = `-- name: GetSubmissionsByIDs :many
SELECT 
    s.id, s.project_id, s.commit_id, s.user_id, s.created_at, s.status, s.reviewed_by, s.reviewed_at, s.review_note, s.content, s.is_deleted,
    p.title AS project_title, p.description AS project_description,
    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 s.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
FROM submissions s
JOIN projects p ON s.project_id = p.id
JOIN users u ON s.user_id = u.id
LEFT JOIN user_profiles up ON u.id = up.user_id
LEFT JOIN users ru ON s.reviewed_by = ru.id
LEFT JOIN user_profiles rup ON ru.id = rup.user_id
WHERE s.id = ANY($1::uuid[]) AND s.is_deleted = false
`

type GetSubmissionsByIDsRow struct {
	ID                 pgtype.UUID        `json:"id"`
	ProjectID          pgtype.UUID        `json:"project_id"`
	CommitID           pgtype.UUID        `json:"commit_id"`
	UserID             pgtype.UUID        `json:"user_id"`
	CreatedAt          pgtype.Timestamptz `json:"created_at"`
	Status             int16              `json:"status"`
	ReviewedBy         pgtype.UUID        `json:"reviewed_by"`
	ReviewedAt         pgtype.Timestamptz `json:"reviewed_at"`
	ReviewNote         pgtype.Text        `json:"review_note"`
	Content            pgtype.Text        `json:"content"`
	IsDeleted          bool               `json:"is_deleted"`
	ProjectTitle       string             `json:"project_title"`
	ProjectDescription pgtype.Text        `json:"project_description"`
	User               []byte             `json:"user"`
	Reviewer           []byte             `json:"reviewer"`
}

func (q *Queries) GetSubmissionsByIDs(ctx context.Context, dollar_1 []pgtype.UUID) ([]GetSubmissionsByIDsRow, error) {
	rows, err := q.db.Query(ctx, getSubmissionsByIDs, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetSubmissionsByIDsRow{}
	for rows.Next() {
		var i GetSubmissionsByIDsRow
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.CommitID,
			&i.UserID,
			&i.CreatedAt,
			&i.Status,
			&i.ReviewedBy,
			&i.ReviewedAt,
			&i.ReviewNote,
			&i.Content,
			&i.IsDeleted,
			&i.ProjectTitle,
			&i.ProjectDescription,
			&i.User,
			&i.Reviewer,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const searchSubmissions = `-- name: SearchSubmissions :many
SELECT 
    s.id, s.project_id, s.commit_id, s.user_id, s.created_at, s.status, s.reviewed_by, s.reviewed_at, s.review_note, s.content, s.is_deleted,
    p.title AS project_title, p.description AS project_description,
    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 s.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
FROM submissions s
JOIN projects p ON s.project_id = p.id
JOIN users u ON s.user_id = u.id
LEFT JOIN user_profiles up ON u.id = up.user_id
LEFT JOIN users ru ON s.reviewed_by = ru.id
LEFT JOIN user_profiles rup ON ru.id = rup.user_id
WHERE s.is_deleted = false
  AND ($1::uuid IS NULL OR s.project_id = $1)
  AND ($2::uuid[] IS NULL OR s.user_id = ANY($2::uuid[]))
  AND ($3::uuid IS NULL OR s.reviewed_by = $3)
  AND (
      $4::smallint[] IS NULL 
      OR s.status = ANY($4::smallint[])
  )
  AND ($5::timestamptz IS NULL OR s.created_at >= $5::timestamptz)
  AND ($6::timestamptz IS NULL OR s.created_at <= $6::timestamptz)
  AND (
      $7::text IS NULL OR 
      s.id::text ILIKE '%' || $7::text || '%' OR
      s.review_note ILIKE '%' || $7::text || '%' OR
      s.content ILIKE '%' || $7::text || '%'
  )
ORDER BY
    CASE WHEN $8 = 'created_at' AND $9 = 'asc' THEN s.created_at END ASC,
    CASE WHEN $8 = 'created_at' AND $9 = 'desc' THEN s.created_at END DESC,
    CASE WHEN $8 = 'reviewed_at' AND $9 = 'asc' THEN s.reviewed_at END ASC,
    CASE WHEN $8 = 'reviewed_at' AND $9 = 'desc' THEN s.reviewed_at END DESC,
    CASE WHEN $8 = 'status' AND $9 = 'asc' THEN s.status END ASC,
    CASE WHEN $8 = 'status' AND $9 = 'desc' THEN s.status END DESC,
    CASE WHEN $8 IS NULL THEN s.created_at END DESC
LIMIT $11
OFFSET $10
`

type SearchSubmissionsParams struct {
	ProjectID   pgtype.UUID        `json:"project_id"`
	UserIds     []pgtype.UUID      `json:"user_ids"`
	ReviewedBy  pgtype.UUID        `json:"reviewed_by"`
	Statuses    []int16            `json:"statuses"`
	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 SearchSubmissionsRow struct {
	ID                 pgtype.UUID        `json:"id"`
	ProjectID          pgtype.UUID        `json:"project_id"`
	CommitID           pgtype.UUID        `json:"commit_id"`
	UserID             pgtype.UUID        `json:"user_id"`
	CreatedAt          pgtype.Timestamptz `json:"created_at"`
	Status             int16              `json:"status"`
	ReviewedBy         pgtype.UUID        `json:"reviewed_by"`
	ReviewedAt         pgtype.Timestamptz `json:"reviewed_at"`
	ReviewNote         pgtype.Text        `json:"review_note"`
	Content            pgtype.Text        `json:"content"`
	IsDeleted          bool               `json:"is_deleted"`
	ProjectTitle       string             `json:"project_title"`
	ProjectDescription pgtype.Text        `json:"project_description"`
	User               []byte             `json:"user"`
	Reviewer           []byte             `json:"reviewer"`
}

func (q *Queries) SearchSubmissions(ctx context.Context, arg SearchSubmissionsParams) ([]SearchSubmissionsRow, error) {
	rows, err := q.db.Query(ctx, searchSubmissions,
		arg.ProjectID,
		arg.UserIds,
		arg.ReviewedBy,
		arg.Statuses,
		arg.CreatedFrom,
		arg.CreatedTo,
		arg.SearchText,
		arg.Sort,
		arg.Order,
		arg.Offset,
		arg.Limit,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []SearchSubmissionsRow{}
	for rows.Next() {
		var i SearchSubmissionsRow
		if err := rows.Scan(
			&i.ID,
			&i.ProjectID,
			&i.CommitID,
			&i.UserID,
			&i.CreatedAt,
			&i.Status,
			&i.ReviewedBy,
			&i.ReviewedAt,
			&i.ReviewNote,
			&i.Content,
			&i.IsDeleted,
			&i.ProjectTitle,
			&i.ProjectDescription,
			&i.User,
			&i.Reviewer,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateSubmission = `-- name: UpdateSubmission :one
UPDATE submissions
SET 
    status = COALESCE($1, status),
    reviewed_by = COALESCE($2, reviewed_by),
    reviewed_at = CASE WHEN $1 IS NOT NULL THEN NOW() ELSE reviewed_at END,
    review_note = COALESCE($3, review_note),
    content = COALESCE($4, content)
WHERE submissions.id = $5
RETURNING 
    submissions.id, submissions.project_id, submissions.commit_id, submissions.user_id, submissions.created_at, submissions.status, submissions.reviewed_by, submissions.reviewed_at, submissions.review_note, submissions.content, submissions.is_deleted,
    (SELECT title FROM projects WHERE id = submissions.project_id) AS project_title,
    (SELECT description FROM projects WHERE id = submissions.project_id) AS project_description,
    (
        SELECT json_build_object(
            'id', u.id,
            'email', u.email,
            'display_name', up.display_name,
            'full_name', up.full_name,
            'avatar_url', up.avatar_url
        )
        FROM users u 
        LEFT JOIN user_profiles up ON u.id = up.user_id
        WHERE u.id = submissions.user_id
    )::json AS user,
    (
        SELECT json_build_object(
            'id', ru.id,
            'email', ru.email,
            'display_name', rup.display_name,
            'full_name', rup.full_name,
            'avatar_url', rup.avatar_url
        )
        FROM users ru 
        LEFT JOIN user_profiles rup ON ru.id = rup.user_id
        WHERE ru.id = submissions.reviewed_by
    )::json AS reviewer
`

type UpdateSubmissionParams struct {
	Status     pgtype.Int2 `json:"status"`
	ReviewedBy pgtype.UUID `json:"reviewed_by"`
	ReviewNote pgtype.Text `json:"review_note"`
	Content    pgtype.Text `json:"content"`
	ID         pgtype.UUID `json:"id"`
}

type UpdateSubmissionRow struct {
	ID                 pgtype.UUID        `json:"id"`
	ProjectID          pgtype.UUID        `json:"project_id"`
	CommitID           pgtype.UUID        `json:"commit_id"`
	UserID             pgtype.UUID        `json:"user_id"`
	CreatedAt          pgtype.Timestamptz `json:"created_at"`
	Status             int16              `json:"status"`
	ReviewedBy         pgtype.UUID        `json:"reviewed_by"`
	ReviewedAt         pgtype.Timestamptz `json:"reviewed_at"`
	ReviewNote         pgtype.Text        `json:"review_note"`
	Content            pgtype.Text        `json:"content"`
	IsDeleted          bool               `json:"is_deleted"`
	ProjectTitle       string             `json:"project_title"`
	ProjectDescription pgtype.Text        `json:"project_description"`
	User               []byte             `json:"user"`
	Reviewer           []byte             `json:"reviewer"`
}

func (q *Queries) UpdateSubmission(ctx context.Context, arg UpdateSubmissionParams) (UpdateSubmissionRow, error) {
	row := q.db.QueryRow(ctx, updateSubmission,
		arg.Status,
		arg.ReviewedBy,
		arg.ReviewNote,
		arg.Content,
		arg.ID,
	)
	var i UpdateSubmissionRow
	err := row.Scan(
		&i.ID,
		&i.ProjectID,
		&i.CommitID,
		&i.UserID,
		&i.CreatedAt,
		&i.Status,
		&i.ReviewedBy,
		&i.ReviewedAt,
		&i.ReviewNote,
		&i.Content,
		&i.IsDeleted,
		&i.ProjectTitle,
		&i.ProjectDescription,
		&i.User,
		&i.Reviewer,
	)
	return i, err
}
