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

package chatsqlc

import (
	"context"

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

const attachToMessage = `-- name: AttachToMessage :exec
UPDATE message_attachments SET message_id = $2 WHERE id = ANY($1::uuid[])
`

type AttachToMessageParams struct {
	Column1   []pgtype.UUID `json:"column_1"`
	MessageID *int64        `json:"message_id"`
}

func (q *Queries) AttachToMessage(ctx context.Context, arg AttachToMessageParams) error {
	_, err := q.db.Exec(ctx, attachToMessage, arg.Column1, arg.MessageID)
	return err
}

const deleteAttachment = `-- name: DeleteAttachment :exec
DELETE FROM message_attachments WHERE id = $1
`

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

const deleteAttachmentsByIDs = `-- name: DeleteAttachmentsByIDs :exec
DELETE FROM message_attachments WHERE id = ANY($1::uuid[])
`

func (q *Queries) DeleteAttachmentsByIDs(ctx context.Context, dollar_1 []pgtype.UUID) error {
	_, err := q.db.Exec(ctx, deleteAttachmentsByIDs, dollar_1)
	return err
}

const getAttachment = `-- name: GetAttachment :one
SELECT id, user_id, message_id, filename, mime_type, size_bytes, sha256, storage_path, created_at FROM message_attachments WHERE id = $1
`

func (q *Queries) GetAttachment(ctx context.Context, id pgtype.UUID) (MessageAttachment, error) {
	row := q.db.QueryRow(ctx, getAttachment, id)
	var i MessageAttachment
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.MessageID,
		&i.Filename,
		&i.MimeType,
		&i.SizeBytes,
		&i.Sha256,
		&i.StoragePath,
		&i.CreatedAt,
	)
	return i, err
}

const insertAttachment = `-- name: InsertAttachment :one
INSERT INTO message_attachments (id, user_id, message_id, filename, mime_type, size_bytes, sha256, storage_path)
VALUES ($1, $2, NULL, $3, $4, $5, $6, $7)
RETURNING id, user_id, message_id, filename, mime_type, size_bytes, sha256, storage_path, created_at
`

type InsertAttachmentParams struct {
	ID          pgtype.UUID `json:"id"`
	UserID      pgtype.UUID `json:"user_id"`
	Filename    string      `json:"filename"`
	MimeType    string      `json:"mime_type"`
	SizeBytes   int64       `json:"size_bytes"`
	Sha256      string      `json:"sha256"`
	StoragePath string      `json:"storage_path"`
}

func (q *Queries) InsertAttachment(ctx context.Context, arg InsertAttachmentParams) (MessageAttachment, error) {
	row := q.db.QueryRow(ctx, insertAttachment,
		arg.ID,
		arg.UserID,
		arg.Filename,
		arg.MimeType,
		arg.SizeBytes,
		arg.Sha256,
		arg.StoragePath,
	)
	var i MessageAttachment
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.MessageID,
		&i.Filename,
		&i.MimeType,
		&i.SizeBytes,
		&i.Sha256,
		&i.StoragePath,
		&i.CreatedAt,
	)
	return i, err
}

const listAttachmentsForMessage = `-- name: ListAttachmentsForMessage :many
SELECT id, user_id, message_id, filename, mime_type, size_bytes, sha256, storage_path, created_at FROM message_attachments WHERE message_id = $1 ORDER BY created_at ASC
`

func (q *Queries) ListAttachmentsForMessage(ctx context.Context, messageID *int64) ([]MessageAttachment, error) {
	rows, err := q.db.Query(ctx, listAttachmentsForMessage, messageID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []MessageAttachment
	for rows.Next() {
		var i MessageAttachment
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.MessageID,
			&i.Filename,
			&i.MimeType,
			&i.SizeBytes,
			&i.Sha256,
			&i.StoragePath,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listAttachmentsForSoftDeletedConversations = `-- name: ListAttachmentsForSoftDeletedConversations :many
SELECT a.id, a.storage_path
FROM message_attachments a
JOIN messages m ON m.id = a.message_id
JOIN conversations c ON c.id = m.conversation_id
WHERE c.deleted_at IS NOT NULL
  AND c.deleted_at < NOW() - ($1::interval)
`

type ListAttachmentsForSoftDeletedConversationsRow struct {
	ID          pgtype.UUID `json:"id"`
	StoragePath string      `json:"storage_path"`
}

func (q *Queries) ListAttachmentsForSoftDeletedConversations(ctx context.Context, dollar_1 pgtype.Interval) ([]ListAttachmentsForSoftDeletedConversationsRow, error) {
	rows, err := q.db.Query(ctx, listAttachmentsForSoftDeletedConversations, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListAttachmentsForSoftDeletedConversationsRow
	for rows.Next() {
		var i ListAttachmentsForSoftDeletedConversationsRow
		if err := rows.Scan(&i.ID, &i.StoragePath); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listExpiredOrphans = `-- name: ListExpiredOrphans :many
SELECT id, storage_path FROM message_attachments
WHERE message_id IS NULL AND created_at < NOW() - ($1::interval)
`

type ListExpiredOrphansRow struct {
	ID          pgtype.UUID `json:"id"`
	StoragePath string      `json:"storage_path"`
}

func (q *Queries) ListExpiredOrphans(ctx context.Context, dollar_1 pgtype.Interval) ([]ListExpiredOrphansRow, error) {
	rows, err := q.db.Query(ctx, listExpiredOrphans, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListExpiredOrphansRow
	for rows.Next() {
		var i ListExpiredOrphansRow
		if err := rows.Scan(&i.ID, &i.StoragePath); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listOrphanAttachmentsForUser = `-- name: ListOrphanAttachmentsForUser :many
SELECT id, user_id, message_id, filename, mime_type, size_bytes, sha256, storage_path, created_at FROM message_attachments
WHERE user_id = $1 AND message_id IS NULL AND id = ANY($2::uuid[])
`

type ListOrphanAttachmentsForUserParams struct {
	UserID  pgtype.UUID   `json:"user_id"`
	Column2 []pgtype.UUID `json:"column_2"`
}

func (q *Queries) ListOrphanAttachmentsForUser(ctx context.Context, arg ListOrphanAttachmentsForUserParams) ([]MessageAttachment, error) {
	rows, err := q.db.Query(ctx, listOrphanAttachmentsForUser, arg.UserID, arg.Column2)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []MessageAttachment
	for rows.Next() {
		var i MessageAttachment
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.MessageID,
			&i.Filename,
			&i.MimeType,
			&i.SizeBytes,
			&i.Sha256,
			&i.StoragePath,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
