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

package gen

import (
	"context"
	"time"

	"github.com/aoagents/agent-orchestrator/backend/internal/domain"
)

const createNotification = `-- name: CreateNotification :one
INSERT INTO notifications (
    id, session_id, project_id, pr_url, type, title, body, status, created_at
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
RETURNING id, session_id, project_id, pr_url, type, title, body, status, created_at
`

type CreateNotificationParams struct {
	ID        string
	SessionID domain.SessionID
	ProjectID domain.ProjectID
	PRURL     string
	Type      domain.NotificationType
	Title     string
	Body      string
	Status    domain.NotificationStatus
	CreatedAt time.Time
}

func (q *Queries) CreateNotification(ctx context.Context, arg CreateNotificationParams) (Notification, error) {
	row := q.db.QueryRowContext(ctx, createNotification,
		arg.ID,
		arg.SessionID,
		arg.ProjectID,
		arg.PRURL,
		arg.Type,
		arg.Title,
		arg.Body,
		arg.Status,
		arg.CreatedAt,
	)
	var i Notification
	err := row.Scan(
		&i.ID,
		&i.SessionID,
		&i.ProjectID,
		&i.PRURL,
		&i.Type,
		&i.Title,
		&i.Body,
		&i.Status,
		&i.CreatedAt,
	)
	return i, err
}

const getUnreadNotificationByDedupe = `-- name: GetUnreadNotificationByDedupe :one
SELECT id, session_id, project_id, pr_url, type, title, body, status, created_at
FROM notifications
WHERE session_id = ? AND type = ? AND pr_url = ? AND status = 'unread'
LIMIT 1
`

type GetUnreadNotificationByDedupeParams struct {
	SessionID domain.SessionID
	Type      domain.NotificationType
	PRURL     string
}

func (q *Queries) GetUnreadNotificationByDedupe(ctx context.Context, arg GetUnreadNotificationByDedupeParams) (Notification, error) {
	row := q.db.QueryRowContext(ctx, getUnreadNotificationByDedupe, arg.SessionID, arg.Type, arg.PRURL)
	var i Notification
	err := row.Scan(
		&i.ID,
		&i.SessionID,
		&i.ProjectID,
		&i.PRURL,
		&i.Type,
		&i.Title,
		&i.Body,
		&i.Status,
		&i.CreatedAt,
	)
	return i, err
}

const listUnreadNotifications = `-- name: ListUnreadNotifications :many
SELECT id, session_id, project_id, pr_url, type, title, body, status, created_at
FROM notifications
WHERE status = 'unread'
ORDER BY created_at DESC
LIMIT ?
`

func (q *Queries) ListUnreadNotifications(ctx context.Context, limit int64) ([]Notification, error) {
	rows, err := q.db.QueryContext(ctx, listUnreadNotifications, limit)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Notification{}
	for rows.Next() {
		var i Notification
		if err := rows.Scan(
			&i.ID,
			&i.SessionID,
			&i.ProjectID,
			&i.PRURL,
			&i.Type,
			&i.Title,
			&i.Body,
			&i.Status,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const markAllNotificationsRead = `-- name: MarkAllNotificationsRead :many
UPDATE notifications
SET status = 'read'
WHERE status = 'unread'
RETURNING id, session_id, project_id, pr_url, type, title, body, status, created_at
`

func (q *Queries) MarkAllNotificationsRead(ctx context.Context) ([]Notification, error) {
	rows, err := q.db.QueryContext(ctx, markAllNotificationsRead)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Notification{}
	for rows.Next() {
		var i Notification
		if err := rows.Scan(
			&i.ID,
			&i.SessionID,
			&i.ProjectID,
			&i.PRURL,
			&i.Type,
			&i.Title,
			&i.Body,
			&i.Status,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const markNotificationRead = `-- name: MarkNotificationRead :one
UPDATE notifications
SET status = 'read'
WHERE id = ? AND status = 'unread'
RETURNING id, session_id, project_id, pr_url, type, title, body, status, created_at
`

func (q *Queries) MarkNotificationRead(ctx context.Context, id string) (Notification, error) {
	row := q.db.QueryRowContext(ctx, markNotificationRead, id)
	var i Notification
	err := row.Scan(
		&i.ID,
		&i.SessionID,
		&i.ProjectID,
		&i.PRURL,
		&i.Type,
		&i.Title,
		&i.Body,
		&i.Status,
		&i.CreatedAt,
	)
	return i, err
}
