// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.31.1
// source: members.sql

package projectsqlc

import (
	"context"

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

const deleteProjectMember = `-- name: DeleteProjectMember :execrows
DELETE FROM project_members
WHERE project_id = $1 AND user_id = $2
`

type DeleteProjectMemberParams struct {
	ProjectID pgtype.UUID `json:"project_id"`
	UserID    pgtype.UUID `json:"user_id"`
}

func (q *Queries) DeleteProjectMember(ctx context.Context, arg DeleteProjectMemberParams) (int64, error) {
	result, err := q.db.Exec(ctx, deleteProjectMember, arg.ProjectID, arg.UserID)
	if err != nil {
		return 0, err
	}
	return result.RowsAffected(), nil
}

const getProjectMember = `-- name: GetProjectMember :one
SELECT project_id, user_id, role, created_at, added_by
FROM project_members
WHERE project_id = $1 AND user_id = $2
`

type GetProjectMemberParams struct {
	ProjectID pgtype.UUID `json:"project_id"`
	UserID    pgtype.UUID `json:"user_id"`
}

func (q *Queries) GetProjectMember(ctx context.Context, arg GetProjectMemberParams) (ProjectMember, error) {
	row := q.db.QueryRow(ctx, getProjectMember, arg.ProjectID, arg.UserID)
	var i ProjectMember
	err := row.Scan(
		&i.ProjectID,
		&i.UserID,
		&i.Role,
		&i.CreatedAt,
		&i.AddedBy,
	)
	return i, err
}

const listProjectMembers = `-- name: ListProjectMembers :many
SELECT project_id, user_id, role, created_at, added_by
FROM project_members
WHERE project_id = $1
ORDER BY created_at ASC
`

func (q *Queries) ListProjectMembers(ctx context.Context, projectID pgtype.UUID) ([]ProjectMember, error) {
	rows, err := q.db.Query(ctx, listProjectMembers, projectID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ProjectMember
	for rows.Next() {
		var i ProjectMember
		if err := rows.Scan(
			&i.ProjectID,
			&i.UserID,
			&i.Role,
			&i.CreatedAt,
			&i.AddedBy,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const upsertProjectMember = `-- name: UpsertProjectMember :one
INSERT INTO project_members (project_id, user_id, role, added_by)
VALUES ($1, $2, $3, $4)
ON CONFLICT (project_id, user_id)
DO UPDATE SET role = EXCLUDED.role
RETURNING project_id, user_id, role, created_at, added_by
`

type UpsertProjectMemberParams struct {
	ProjectID pgtype.UUID `json:"project_id"`
	UserID    pgtype.UUID `json:"user_id"`
	Role      string      `json:"role"`
	AddedBy   pgtype.UUID `json:"added_by"`
}

func (q *Queries) UpsertProjectMember(ctx context.Context, arg UpsertProjectMemberParams) (ProjectMember, error) {
	row := q.db.QueryRow(ctx, upsertProjectMember,
		arg.ProjectID,
		arg.UserID,
		arg.Role,
		arg.AddedBy,
	)
	var i ProjectMember
	err := row.Scan(
		&i.ProjectID,
		&i.UserID,
		&i.Role,
		&i.CreatedAt,
		&i.AddedBy,
	)
	return i, err
}
