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

package db

import (
	"context"

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

const addIssueSubscriber = `-- name: AddIssueSubscriber :exec
INSERT INTO issue_subscriber (issue_id, user_type, user_id, reason)
VALUES ($1, $2, $3, $4)
ON CONFLICT (issue_id, user_type, user_id) DO NOTHING
`

type AddIssueSubscriberParams struct {
	IssueID  pgtype.UUID `json:"issue_id"`
	UserType string      `json:"user_type"`
	UserID   pgtype.UUID `json:"user_id"`
	Reason   string      `json:"reason"`
}

func (q *Queries) AddIssueSubscriber(ctx context.Context, arg AddIssueSubscriberParams) error {
	_, err := q.db.Exec(ctx, addIssueSubscriber,
		arg.IssueID,
		arg.UserType,
		arg.UserID,
		arg.Reason,
	)
	return err
}

const isIssueSubscriber = `-- name: IsIssueSubscriber :one
SELECT EXISTS(
    SELECT 1 FROM issue_subscriber
    WHERE issue_id = $1 AND user_type = $2 AND user_id = $3
) AS subscribed
`

type IsIssueSubscriberParams struct {
	IssueID  pgtype.UUID `json:"issue_id"`
	UserType string      `json:"user_type"`
	UserID   pgtype.UUID `json:"user_id"`
}

func (q *Queries) IsIssueSubscriber(ctx context.Context, arg IsIssueSubscriberParams) (bool, error) {
	row := q.db.QueryRow(ctx, isIssueSubscriber, arg.IssueID, arg.UserType, arg.UserID)
	var subscribed bool
	err := row.Scan(&subscribed)
	return subscribed, err
}

const listIssueSubscribers = `-- name: ListIssueSubscribers :many
SELECT issue_id, user_type, user_id, reason, created_at FROM issue_subscriber
WHERE issue_id = $1
ORDER BY created_at
`

func (q *Queries) ListIssueSubscribers(ctx context.Context, issueID pgtype.UUID) ([]IssueSubscriber, error) {
	rows, err := q.db.Query(ctx, listIssueSubscribers, issueID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []IssueSubscriber{}
	for rows.Next() {
		var i IssueSubscriber
		if err := rows.Scan(
			&i.IssueID,
			&i.UserType,
			&i.UserID,
			&i.Reason,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const removeIssueSubscriber = `-- name: RemoveIssueSubscriber :exec
DELETE FROM issue_subscriber
WHERE issue_id = $1 AND user_type = $2 AND user_id = $3
`

type RemoveIssueSubscriberParams struct {
	IssueID  pgtype.UUID `json:"issue_id"`
	UserType string      `json:"user_type"`
	UserID   pgtype.UUID `json:"user_id"`
}

func (q *Queries) RemoveIssueSubscriber(ctx context.Context, arg RemoveIssueSubscriberParams) error {
	_, err := q.db.Exec(ctx, removeIssueSubscriber, arg.IssueID, arg.UserType, arg.UserID)
	return err
}
