// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.29.0
// source: organizations.sql

package configdb

import (
	"context"

	"github.com/google/uuid"
)

const clearOrganizations = `-- name: ClearOrganizations :exec
DELETE FROM organizations
`

func (q *Queries) ClearOrganizations(ctx context.Context) error {
	_, err := q.db.Exec(ctx, clearOrganizations)
	return err
}

const deleteOrganization = `-- name: DeleteOrganization :exec
DELETE FROM organizations 
WHERE id = $1
`

func (q *Queries) DeleteOrganization(ctx context.Context, id uuid.UUID) error {
	_, err := q.db.Exec(ctx, deleteOrganization, id)
	return err
}

const getOrganization = `-- name: GetOrganization :one
SELECT id, name, enabled, created_at, synced_at FROM organizations 
WHERE id = $1
`

func (q *Queries) GetOrganization(ctx context.Context, id uuid.UUID) (Organization, error) {
	row := q.db.QueryRow(ctx, getOrganization, id)
	var i Organization
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Enabled,
		&i.CreatedAt,
		&i.SyncedAt,
	)
	return i, err
}

const getOrganizationByName = `-- name: GetOrganizationByName :one
SELECT id, name, enabled, created_at, synced_at FROM organizations 
WHERE name = $1
`

func (q *Queries) GetOrganizationByName(ctx context.Context, name string) (Organization, error) {
	row := q.db.QueryRow(ctx, getOrganizationByName, name)
	var i Organization
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Enabled,
		&i.CreatedAt,
		&i.SyncedAt,
	)
	return i, err
}

const listEnabledOrganizations = `-- name: ListEnabledOrganizations :many
SELECT id, name, enabled, created_at, synced_at FROM organizations
WHERE enabled = true
ORDER BY name
`

func (q *Queries) ListEnabledOrganizations(ctx context.Context) ([]Organization, error) {
	rows, err := q.db.Query(ctx, listEnabledOrganizations)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Organization
	for rows.Next() {
		var i Organization
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Enabled,
			&i.CreatedAt,
			&i.SyncedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const listOrganizations = `-- name: ListOrganizations :many
SELECT id, name, enabled, created_at, synced_at FROM organizations
ORDER BY name
`

func (q *Queries) ListOrganizations(ctx context.Context) ([]Organization, error) {
	rows, err := q.db.Query(ctx, listOrganizations)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []Organization
	for rows.Next() {
		var i Organization
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Enabled,
			&i.CreatedAt,
			&i.SyncedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const upsertOrganization = `-- name: UpsertOrganization :one
INSERT INTO organizations (
  id, name, enabled, synced_at
) VALUES (
  $1, $2, $3, NOW()
) ON CONFLICT (id) DO UPDATE SET
  name = EXCLUDED.name,
  enabled = EXCLUDED.enabled,
  synced_at = NOW()
RETURNING id, name, enabled, created_at, synced_at
`

type UpsertOrganizationParams struct {
	ID      uuid.UUID `json:"id"`
	Name    string    `json:"name"`
	Enabled bool      `json:"enabled"`
}

func (q *Queries) UpsertOrganization(ctx context.Context, arg UpsertOrganizationParams) (Organization, error) {
	row := q.db.QueryRow(ctx, upsertOrganization, arg.ID, arg.Name, arg.Enabled)
	var i Organization
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Enabled,
		&i.CreatedAt,
		&i.SyncedAt,
	)
	return i, err
}
