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

package repository

import (
	"context"

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

const createCalendarSubscription = `-- name: CreateCalendarSubscription :one
INSERT INTO calendar_subscriptions (id, calendar_id, source_url, sync_interval_minutes)
VALUES ($1, $2, $3, $4)
RETURNING id, calendar_id, source_url, last_synced_at, sync_interval_minutes, created_at
`

type CreateCalendarSubscriptionParams struct {
	ID                  pgtype.UUID `json:"id"`
	CalendarID          pgtype.UUID `json:"calendar_id"`
	SourceUrl           string      `json:"source_url"`
	SyncIntervalMinutes pgtype.Int4 `json:"sync_interval_minutes"`
}

func (q *Queries) CreateCalendarSubscription(ctx context.Context, arg CreateCalendarSubscriptionParams) (CalendarSubscription, error) {
	row := q.db.QueryRow(ctx, createCalendarSubscription,
		arg.ID,
		arg.CalendarID,
		arg.SourceUrl,
		arg.SyncIntervalMinutes,
	)
	var i CalendarSubscription
	err := row.Scan(
		&i.ID,
		&i.CalendarID,
		&i.SourceUrl,
		&i.LastSyncedAt,
		&i.SyncIntervalMinutes,
		&i.CreatedAt,
	)
	return i, err
}

const deleteSubscription = `-- name: DeleteSubscription :exec
DELETE FROM calendar_subscriptions
WHERE id = $1
`

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

const getSubscriptionByID = `-- name: GetSubscriptionByID :one
SELECT id, calendar_id, source_url, last_synced_at, sync_interval_minutes, created_at FROM calendar_subscriptions
WHERE id = $1
`

func (q *Queries) GetSubscriptionByID(ctx context.Context, id pgtype.UUID) (CalendarSubscription, error) {
	row := q.db.QueryRow(ctx, getSubscriptionByID, id)
	var i CalendarSubscription
	err := row.Scan(
		&i.ID,
		&i.CalendarID,
		&i.SourceUrl,
		&i.LastSyncedAt,
		&i.SyncIntervalMinutes,
		&i.CreatedAt,
	)
	return i, err
}

const listSubscriptionsByCalendar = `-- name: ListSubscriptionsByCalendar :many
SELECT id, calendar_id, source_url, last_synced_at, sync_interval_minutes, created_at FROM calendar_subscriptions
WHERE calendar_id = $1
ORDER BY created_at ASC
`

func (q *Queries) ListSubscriptionsByCalendar(ctx context.Context, calendarID pgtype.UUID) ([]CalendarSubscription, error) {
	rows, err := q.db.Query(ctx, listSubscriptionsByCalendar, calendarID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []CalendarSubscription{}
	for rows.Next() {
		var i CalendarSubscription
		if err := rows.Scan(
			&i.ID,
			&i.CalendarID,
			&i.SourceUrl,
			&i.LastSyncedAt,
			&i.SyncIntervalMinutes,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateSubscriptionLastSynced = `-- name: UpdateSubscriptionLastSynced :exec
UPDATE calendar_subscriptions
SET last_synced_at = now()
WHERE id = $1
`

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

const listSubscriptionsDueForSync = `-- name: ListSubscriptionsDueForSync :many
SELECT s.id, s.calendar_id, s.source_url, c.owner_id
FROM calendar_subscriptions s
JOIN calendars c ON c.id = s.calendar_id AND c.deleted_at IS NULL
WHERE s.sync_interval_minutes IS NOT NULL
  AND s.sync_interval_minutes > 0
  AND (s.last_synced_at IS NULL
       OR s.last_synced_at + make_interval(mins => s.sync_interval_minutes::numeric) <= now())
`

type ListSubscriptionsDueForSyncRow struct {
	ID         pgtype.UUID `json:"id"`
	CalendarID pgtype.UUID `json:"calendar_id"`
	SourceUrl  string      `json:"source_url"`
	OwnerID    pgtype.UUID `json:"owner_id"`
}

func (q *Queries) ListSubscriptionsDueForSync(ctx context.Context) ([]ListSubscriptionsDueForSyncRow, error) {
	rows, err := q.db.Query(ctx, listSubscriptionsDueForSync)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ListSubscriptionsDueForSyncRow
	for rows.Next() {
		var i ListSubscriptionsDueForSyncRow
		if err := rows.Scan(&i.ID, &i.CalendarID, &i.SourceUrl, &i.OwnerID); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}
