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

package sqlc

import (
	"context"

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

const createSchedule = `-- name: CreateSchedule :one
INSERT INTO schedule (name, description, pattern, max_calls, enabled, command, bot_id)
VALUES ($1, $2, $3, $4, $5, $6, $7)
RETURNING id, name, description, pattern, max_calls, current_calls, created_at, updated_at, enabled, command, bot_id
`

type CreateScheduleParams struct {
	Name        string      `json:"name"`
	Description string      `json:"description"`
	Pattern     string      `json:"pattern"`
	MaxCalls    pgtype.Int4 `json:"max_calls"`
	Enabled     bool        `json:"enabled"`
	Command     string      `json:"command"`
	BotID       pgtype.UUID `json:"bot_id"`
}

func (q *Queries) CreateSchedule(ctx context.Context, arg CreateScheduleParams) (Schedule, error) {
	row := q.db.QueryRow(ctx, createSchedule,
		arg.Name,
		arg.Description,
		arg.Pattern,
		arg.MaxCalls,
		arg.Enabled,
		arg.Command,
		arg.BotID,
	)
	var i Schedule
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Description,
		&i.Pattern,
		&i.MaxCalls,
		&i.CurrentCalls,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Enabled,
		&i.Command,
		&i.BotID,
	)
	return i, err
}

const deleteSchedule = `-- name: DeleteSchedule :exec
DELETE FROM schedule
WHERE id = $1
`

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

const getScheduleByID = `-- name: GetScheduleByID :one
SELECT id, name, description, pattern, max_calls, current_calls, created_at, updated_at, enabled, command, bot_id
FROM schedule
WHERE id = $1
`

func (q *Queries) GetScheduleByID(ctx context.Context, id pgtype.UUID) (Schedule, error) {
	row := q.db.QueryRow(ctx, getScheduleByID, id)
	var i Schedule
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Description,
		&i.Pattern,
		&i.MaxCalls,
		&i.CurrentCalls,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Enabled,
		&i.Command,
		&i.BotID,
	)
	return i, err
}

const incrementScheduleCalls = `-- name: IncrementScheduleCalls :one
UPDATE schedule
SET current_calls = current_calls + 1,
    enabled = CASE
      WHEN max_calls IS NOT NULL AND current_calls + 1 >= max_calls THEN false
      ELSE enabled
    END,
    updated_at = now()
WHERE id = $1
RETURNING id, name, description, pattern, max_calls, current_calls, created_at, updated_at, enabled, command, bot_id
`

func (q *Queries) IncrementScheduleCalls(ctx context.Context, id pgtype.UUID) (Schedule, error) {
	row := q.db.QueryRow(ctx, incrementScheduleCalls, id)
	var i Schedule
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Description,
		&i.Pattern,
		&i.MaxCalls,
		&i.CurrentCalls,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Enabled,
		&i.Command,
		&i.BotID,
	)
	return i, err
}

const listEnabledSchedules = `-- name: ListEnabledSchedules :many
SELECT id, name, description, pattern, max_calls, current_calls, created_at, updated_at, enabled, command, bot_id
FROM schedule
WHERE enabled = true
ORDER BY created_at DESC
`

func (q *Queries) ListEnabledSchedules(ctx context.Context) ([]Schedule, error) {
	rows, err := q.db.Query(ctx, listEnabledSchedules)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Schedule
	for rows.Next() {
		var i Schedule
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Description,
			&i.Pattern,
			&i.MaxCalls,
			&i.CurrentCalls,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.Enabled,
			&i.Command,
			&i.BotID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listSchedulesByBot = `-- name: ListSchedulesByBot :many
SELECT id, name, description, pattern, max_calls, current_calls, created_at, updated_at, enabled, command, bot_id
FROM schedule
WHERE bot_id = $1
ORDER BY created_at DESC
`

func (q *Queries) ListSchedulesByBot(ctx context.Context, botID pgtype.UUID) ([]Schedule, error) {
	rows, err := q.db.Query(ctx, listSchedulesByBot, botID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Schedule
	for rows.Next() {
		var i Schedule
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Description,
			&i.Pattern,
			&i.MaxCalls,
			&i.CurrentCalls,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.Enabled,
			&i.Command,
			&i.BotID,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateSchedule = `-- name: UpdateSchedule :one
UPDATE schedule
SET name = $2,
    description = $3,
    pattern = $4,
    max_calls = $5,
    enabled = $6,
    command = $7,
    updated_at = now()
WHERE id = $1
RETURNING id, name, description, pattern, max_calls, current_calls, created_at, updated_at, enabled, command, bot_id
`

type UpdateScheduleParams struct {
	ID          pgtype.UUID `json:"id"`
	Name        string      `json:"name"`
	Description string      `json:"description"`
	Pattern     string      `json:"pattern"`
	MaxCalls    pgtype.Int4 `json:"max_calls"`
	Enabled     bool        `json:"enabled"`
	Command     string      `json:"command"`
}

func (q *Queries) UpdateSchedule(ctx context.Context, arg UpdateScheduleParams) (Schedule, error) {
	row := q.db.QueryRow(ctx, updateSchedule,
		arg.ID,
		arg.Name,
		arg.Description,
		arg.Pattern,
		arg.MaxCalls,
		arg.Enabled,
		arg.Command,
	)
	var i Schedule
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Description,
		&i.Pattern,
		&i.MaxCalls,
		&i.CurrentCalls,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.Enabled,
		&i.Command,
		&i.BotID,
	)
	return i, err
}
