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

package repository

import (
	"context"

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

const deleteAllCalendarMembers = `-- name: DeleteAllCalendarMembers :exec
DELETE FROM calendar_members
WHERE calendar_id = $1
`

func (q *Queries) DeleteAllCalendarMembers(ctx context.Context, calendarID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteAllCalendarMembers, calendarID)
	return err
}

const deleteCalendarMember = `-- name: DeleteCalendarMember :exec
DELETE FROM calendar_members
WHERE calendar_id = $1 AND user_id = $2
`

type DeleteCalendarMemberParams struct {
	CalendarID pgtype.UUID `json:"calendar_id"`
	UserID     pgtype.UUID `json:"user_id"`
}

func (q *Queries) DeleteCalendarMember(ctx context.Context, arg DeleteCalendarMemberParams) error {
	_, err := q.db.Exec(ctx, deleteCalendarMember, arg.CalendarID, arg.UserID)
	return err
}

const getCalendarMemberRole = `-- name: GetCalendarMemberRole :one
SELECT role FROM calendar_members
WHERE calendar_id = $1 AND user_id = $2
`

type GetCalendarMemberRoleParams struct {
	CalendarID pgtype.UUID `json:"calendar_id"`
	UserID     pgtype.UUID `json:"user_id"`
}

func (q *Queries) GetCalendarMemberRole(ctx context.Context, arg GetCalendarMemberRoleParams) (string, error) {
	row := q.db.QueryRow(ctx, getCalendarMemberRole, arg.CalendarID, arg.UserID)
	var role string
	err := row.Scan(&role)
	return role, err
}

const listCalendarMembers = `-- name: ListCalendarMembers :many
SELECT cm.user_id, u.email, cm.role
FROM calendar_members cm
JOIN users u ON u.id = cm.user_id
WHERE cm.calendar_id = $1 AND u.deleted_at IS NULL
ORDER BY cm.role ASC
`

type ListCalendarMembersRow struct {
	UserID pgtype.UUID `json:"user_id"`
	Email  string      `json:"email"`
	Role   string      `json:"role"`
}

func (q *Queries) ListCalendarMembers(ctx context.Context, calendarID pgtype.UUID) ([]ListCalendarMembersRow, error) {
	rows, err := q.db.Query(ctx, listCalendarMembers, calendarID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListCalendarMembersRow{}
	for rows.Next() {
		var i ListCalendarMembersRow
		if err := rows.Scan(&i.UserID, &i.Email, &i.Role); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const upsertCalendarMember = `-- name: UpsertCalendarMember :exec
INSERT INTO calendar_members (calendar_id, user_id, role)
VALUES ($1, $2, $3)
ON CONFLICT (calendar_id, user_id) DO UPDATE SET role = $3
`

type UpsertCalendarMemberParams struct {
	CalendarID pgtype.UUID `json:"calendar_id"`
	UserID     pgtype.UUID `json:"user_id"`
	Role       string      `json:"role"`
}

func (q *Queries) UpsertCalendarMember(ctx context.Context, arg UpsertCalendarMemberParams) error {
	_, err := q.db.Exec(ctx, upsertCalendarMember, arg.CalendarID, arg.UserID, arg.Role)
	return err
}
