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

package sqlc

import (
	"context"

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

const deleteBotACLGuestAllAllowRule = `-- name: DeleteBotACLGuestAllAllowRule :exec
DELETE FROM bot_acl_rules
WHERE bot_id = $1
  AND action = 'chat.trigger'
  AND effect = 'allow'
  AND subject_kind = 'guest_all'
`

func (q *Queries) DeleteBotACLGuestAllAllowRule(ctx context.Context, botID pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteBotACLGuestAllAllowRule, botID)
	return err
}

const deleteBotACLRuleByID = `-- name: DeleteBotACLRuleByID :exec
DELETE FROM bot_acl_rules
WHERE id = $1
`

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

const hasBotACLChannelIdentityRule = `-- name: HasBotACLChannelIdentityRule :one
SELECT EXISTS (
  SELECT 1
  FROM bot_acl_rules
  WHERE bot_id = $1
    AND action = 'chat.trigger'
    AND effect = $2
    AND subject_kind = 'channel_identity'
    AND channel_identity_id = $3
    AND (source_channel IS NULL OR source_channel = $4::text)
    AND (source_conversation_type IS NULL OR source_conversation_type = $5::text)
    AND (source_conversation_id IS NULL OR source_conversation_id = $6::text)
    AND (source_thread_id IS NULL OR source_thread_id = $7::text)
) AS matched
`

type HasBotACLChannelIdentityRuleParams struct {
	BotID                  pgtype.UUID `json:"bot_id"`
	Effect                 string      `json:"effect"`
	ChannelIdentityID      pgtype.UUID `json:"channel_identity_id"`
	SourceChannel          pgtype.Text `json:"source_channel"`
	SourceConversationType pgtype.Text `json:"source_conversation_type"`
	SourceConversationID   pgtype.Text `json:"source_conversation_id"`
	SourceThreadID         pgtype.Text `json:"source_thread_id"`
}

func (q *Queries) HasBotACLChannelIdentityRule(ctx context.Context, arg HasBotACLChannelIdentityRuleParams) (bool, error) {
	row := q.db.QueryRow(ctx, hasBotACLChannelIdentityRule,
		arg.BotID,
		arg.Effect,
		arg.ChannelIdentityID,
		arg.SourceChannel,
		arg.SourceConversationType,
		arg.SourceConversationID,
		arg.SourceThreadID,
	)
	var matched bool
	err := row.Scan(&matched)
	return matched, err
}

const hasBotACLGuestAllAllowRule = `-- name: HasBotACLGuestAllAllowRule :one
SELECT EXISTS (
  SELECT 1
  FROM bot_acl_rules
  WHERE bot_id = $1
    AND action = 'chat.trigger'
    AND effect = 'allow'
    AND subject_kind = 'guest_all'
) AS allowed
`

func (q *Queries) HasBotACLGuestAllAllowRule(ctx context.Context, botID pgtype.UUID) (bool, error) {
	row := q.db.QueryRow(ctx, hasBotACLGuestAllAllowRule, botID)
	var allowed bool
	err := row.Scan(&allowed)
	return allowed, err
}

const hasBotACLUserRule = `-- name: HasBotACLUserRule :one
SELECT EXISTS (
  SELECT 1
  FROM bot_acl_rules
  WHERE bot_id = $1
    AND action = 'chat.trigger'
    AND effect = $2
    AND subject_kind = 'user'
    AND user_id = $3
    AND (source_channel IS NULL OR source_channel = $4::text)
    AND (source_conversation_type IS NULL OR source_conversation_type = $5::text)
    AND (source_conversation_id IS NULL OR source_conversation_id = $6::text)
    AND (source_thread_id IS NULL OR source_thread_id = $7::text)
) AS matched
`

type HasBotACLUserRuleParams struct {
	BotID                  pgtype.UUID `json:"bot_id"`
	Effect                 string      `json:"effect"`
	UserID                 pgtype.UUID `json:"user_id"`
	SourceChannel          pgtype.Text `json:"source_channel"`
	SourceConversationType pgtype.Text `json:"source_conversation_type"`
	SourceConversationID   pgtype.Text `json:"source_conversation_id"`
	SourceThreadID         pgtype.Text `json:"source_thread_id"`
}

func (q *Queries) HasBotACLUserRule(ctx context.Context, arg HasBotACLUserRuleParams) (bool, error) {
	row := q.db.QueryRow(ctx, hasBotACLUserRule,
		arg.BotID,
		arg.Effect,
		arg.UserID,
		arg.SourceChannel,
		arg.SourceConversationType,
		arg.SourceConversationID,
		arg.SourceThreadID,
	)
	var matched bool
	err := row.Scan(&matched)
	return matched, err
}

const listBotACLSubjectRulesByEffect = `-- name: ListBotACLSubjectRulesByEffect :many
SELECT
  r.id,
  r.bot_id,
  r.action,
  r.effect,
  r.subject_kind,
  r.user_id,
  r.channel_identity_id,
  r.source_channel,
  r.source_conversation_type,
  r.source_conversation_id,
  r.source_thread_id,
  r.created_by_user_id,
  r.created_at,
  r.updated_at,
  u.username AS user_username,
  u.display_name AS user_display_name,
  u.avatar_url AS user_avatar_url,
  ci.channel_type,
  ci.channel_subject_id,
  ci.display_name AS channel_identity_display_name,
  ci.avatar_url AS channel_identity_avatar_url,
  linked.id AS linked_user_id,
  linked.username AS linked_user_username,
  linked.display_name AS linked_user_display_name,
  linked.avatar_url AS linked_user_avatar_url
FROM bot_acl_rules r
LEFT JOIN users u ON u.id = r.user_id
LEFT JOIN channel_identities ci ON ci.id = r.channel_identity_id
LEFT JOIN users linked ON linked.id = ci.user_id
WHERE r.bot_id = $1
  AND r.action = 'chat.trigger'
  AND r.effect = $2
  AND r.subject_kind IN ('user', 'channel_identity')
ORDER BY r.created_at DESC
`

type ListBotACLSubjectRulesByEffectParams struct {
	BotID  pgtype.UUID `json:"bot_id"`
	Effect string      `json:"effect"`
}

type ListBotACLSubjectRulesByEffectRow struct {
	ID                         pgtype.UUID        `json:"id"`
	BotID                      pgtype.UUID        `json:"bot_id"`
	Action                     string             `json:"action"`
	Effect                     string             `json:"effect"`
	SubjectKind                string             `json:"subject_kind"`
	UserID                     pgtype.UUID        `json:"user_id"`
	ChannelIdentityID          pgtype.UUID        `json:"channel_identity_id"`
	SourceChannel              pgtype.Text        `json:"source_channel"`
	SourceConversationType     pgtype.Text        `json:"source_conversation_type"`
	SourceConversationID       pgtype.Text        `json:"source_conversation_id"`
	SourceThreadID             pgtype.Text        `json:"source_thread_id"`
	CreatedByUserID            pgtype.UUID        `json:"created_by_user_id"`
	CreatedAt                  pgtype.Timestamptz `json:"created_at"`
	UpdatedAt                  pgtype.Timestamptz `json:"updated_at"`
	UserUsername               pgtype.Text        `json:"user_username"`
	UserDisplayName            pgtype.Text        `json:"user_display_name"`
	UserAvatarUrl              pgtype.Text        `json:"user_avatar_url"`
	ChannelType                pgtype.Text        `json:"channel_type"`
	ChannelSubjectID           pgtype.Text        `json:"channel_subject_id"`
	ChannelIdentityDisplayName pgtype.Text        `json:"channel_identity_display_name"`
	ChannelIdentityAvatarUrl   pgtype.Text        `json:"channel_identity_avatar_url"`
	LinkedUserID               pgtype.UUID        `json:"linked_user_id"`
	LinkedUserUsername         pgtype.Text        `json:"linked_user_username"`
	LinkedUserDisplayName      pgtype.Text        `json:"linked_user_display_name"`
	LinkedUserAvatarUrl        pgtype.Text        `json:"linked_user_avatar_url"`
}

func (q *Queries) ListBotACLSubjectRulesByEffect(ctx context.Context, arg ListBotACLSubjectRulesByEffectParams) ([]ListBotACLSubjectRulesByEffectRow, error) {
	rows, err := q.db.Query(ctx, listBotACLSubjectRulesByEffect, arg.BotID, arg.Effect)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListBotACLSubjectRulesByEffectRow
	for rows.Next() {
		var i ListBotACLSubjectRulesByEffectRow
		if err := rows.Scan(
			&i.ID,
			&i.BotID,
			&i.Action,
			&i.Effect,
			&i.SubjectKind,
			&i.UserID,
			&i.ChannelIdentityID,
			&i.SourceChannel,
			&i.SourceConversationType,
			&i.SourceConversationID,
			&i.SourceThreadID,
			&i.CreatedByUserID,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.UserUsername,
			&i.UserDisplayName,
			&i.UserAvatarUrl,
			&i.ChannelType,
			&i.ChannelSubjectID,
			&i.ChannelIdentityDisplayName,
			&i.ChannelIdentityAvatarUrl,
			&i.LinkedUserID,
			&i.LinkedUserUsername,
			&i.LinkedUserDisplayName,
			&i.LinkedUserAvatarUrl,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const upsertBotACLChannelIdentityRule = `-- name: UpsertBotACLChannelIdentityRule :one
INSERT INTO bot_acl_rules (
  bot_id, action, effect, subject_kind, channel_identity_id,
  source_channel, source_conversation_type, source_conversation_id, source_thread_id,
  created_by_user_id
)
VALUES (
  $1, 'chat.trigger', $2, 'channel_identity', $3,
  $5::text,
  $6::text,
  $7::text,
  $8::text,
  $4
)
ON CONFLICT ON CONSTRAINT bot_acl_rules_unique_channel_identity
DO UPDATE SET
  created_by_user_id = COALESCE(EXCLUDED.created_by_user_id, bot_acl_rules.created_by_user_id),
  updated_at = now()
RETURNING id, bot_id, action, effect, subject_kind, user_id, channel_identity_id, source_channel, source_conversation_type, source_conversation_id, source_thread_id, created_by_user_id, created_at, updated_at
`

type UpsertBotACLChannelIdentityRuleParams struct {
	BotID                  pgtype.UUID `json:"bot_id"`
	Effect                 string      `json:"effect"`
	ChannelIdentityID      pgtype.UUID `json:"channel_identity_id"`
	CreatedByUserID        pgtype.UUID `json:"created_by_user_id"`
	SourceChannel          pgtype.Text `json:"source_channel"`
	SourceConversationType pgtype.Text `json:"source_conversation_type"`
	SourceConversationID   pgtype.Text `json:"source_conversation_id"`
	SourceThreadID         pgtype.Text `json:"source_thread_id"`
}

func (q *Queries) UpsertBotACLChannelIdentityRule(ctx context.Context, arg UpsertBotACLChannelIdentityRuleParams) (BotAclRule, error) {
	row := q.db.QueryRow(ctx, upsertBotACLChannelIdentityRule,
		arg.BotID,
		arg.Effect,
		arg.ChannelIdentityID,
		arg.CreatedByUserID,
		arg.SourceChannel,
		arg.SourceConversationType,
		arg.SourceConversationID,
		arg.SourceThreadID,
	)
	var i BotAclRule
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.Action,
		&i.Effect,
		&i.SubjectKind,
		&i.UserID,
		&i.ChannelIdentityID,
		&i.SourceChannel,
		&i.SourceConversationType,
		&i.SourceConversationID,
		&i.SourceThreadID,
		&i.CreatedByUserID,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const upsertBotACLGuestAllAllowRule = `-- name: UpsertBotACLGuestAllAllowRule :one
INSERT INTO bot_acl_rules (bot_id, action, effect, subject_kind, created_by_user_id)
VALUES ($1, 'chat.trigger', 'allow', 'guest_all', $2)
ON CONFLICT ON CONSTRAINT bot_acl_rules_unique_user
DO UPDATE SET
  created_by_user_id = COALESCE(EXCLUDED.created_by_user_id, bot_acl_rules.created_by_user_id),
  updated_at = now()
RETURNING id, bot_id, action, effect, subject_kind, user_id, channel_identity_id, source_channel, source_conversation_type, source_conversation_id, source_thread_id, created_by_user_id, created_at, updated_at
`

type UpsertBotACLGuestAllAllowRuleParams struct {
	BotID           pgtype.UUID `json:"bot_id"`
	CreatedByUserID pgtype.UUID `json:"created_by_user_id"`
}

func (q *Queries) UpsertBotACLGuestAllAllowRule(ctx context.Context, arg UpsertBotACLGuestAllAllowRuleParams) (BotAclRule, error) {
	row := q.db.QueryRow(ctx, upsertBotACLGuestAllAllowRule, arg.BotID, arg.CreatedByUserID)
	var i BotAclRule
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.Action,
		&i.Effect,
		&i.SubjectKind,
		&i.UserID,
		&i.ChannelIdentityID,
		&i.SourceChannel,
		&i.SourceConversationType,
		&i.SourceConversationID,
		&i.SourceThreadID,
		&i.CreatedByUserID,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const upsertBotACLUserRule = `-- name: UpsertBotACLUserRule :one
INSERT INTO bot_acl_rules (
  bot_id, action, effect, subject_kind, user_id,
  source_channel, source_conversation_type, source_conversation_id, source_thread_id,
  created_by_user_id
)
VALUES (
  $1, 'chat.trigger', $2, 'user', $3,
  $5::text,
  $6::text,
  $7::text,
  $8::text,
  $4
)
ON CONFLICT ON CONSTRAINT bot_acl_rules_unique_user
DO UPDATE SET
  created_by_user_id = COALESCE(EXCLUDED.created_by_user_id, bot_acl_rules.created_by_user_id),
  updated_at = now()
RETURNING id, bot_id, action, effect, subject_kind, user_id, channel_identity_id, source_channel, source_conversation_type, source_conversation_id, source_thread_id, created_by_user_id, created_at, updated_at
`

type UpsertBotACLUserRuleParams struct {
	BotID                  pgtype.UUID `json:"bot_id"`
	Effect                 string      `json:"effect"`
	UserID                 pgtype.UUID `json:"user_id"`
	CreatedByUserID        pgtype.UUID `json:"created_by_user_id"`
	SourceChannel          pgtype.Text `json:"source_channel"`
	SourceConversationType pgtype.Text `json:"source_conversation_type"`
	SourceConversationID   pgtype.Text `json:"source_conversation_id"`
	SourceThreadID         pgtype.Text `json:"source_thread_id"`
}

func (q *Queries) UpsertBotACLUserRule(ctx context.Context, arg UpsertBotACLUserRuleParams) (BotAclRule, error) {
	row := q.db.QueryRow(ctx, upsertBotACLUserRule,
		arg.BotID,
		arg.Effect,
		arg.UserID,
		arg.CreatedByUserID,
		arg.SourceChannel,
		arg.SourceConversationType,
		arg.SourceConversationID,
		arg.SourceThreadID,
	)
	var i BotAclRule
	err := row.Scan(
		&i.ID,
		&i.BotID,
		&i.Action,
		&i.Effect,
		&i.SubjectKind,
		&i.UserID,
		&i.ChannelIdentityID,
		&i.SourceChannel,
		&i.SourceConversationType,
		&i.SourceConversationID,
		&i.SourceThreadID,
		&i.CreatedByUserID,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
