// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.27.0
// source: templates.sql

package sqlcdb

import (
	"context"
	"database/sql"

	"github.com/google/uuid"
)

const countServicesByProjectAndName = `-- name: CountServicesByProjectAndName :one
SELECT COUNT(*)
FROM services
WHERE project_id = $1 AND name = $2
`

type CountServicesByProjectAndNameParams struct {
	ProjectID uuid.UUID `json:"project_id"`
	Name      string    `json:"name"`
}

func (q *Queries) CountServicesByProjectAndName(ctx context.Context, arg CountServicesByProjectAndNameParams) (int64, error) {
	row := q.db.QueryRowContext(ctx, countServicesByProjectAndName, arg.ProjectID, arg.Name)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const createServiceFromTemplate = `-- name: CreateServiceFromTemplate :exec
INSERT INTO services (id, project_id, name, type, status, image, command, environment, cpu, memory, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)
`

type CreateServiceFromTemplateParams struct {
	ID          uuid.UUID      `json:"id"`
	ProjectID   uuid.UUID      `json:"project_id"`
	Name        string         `json:"name"`
	Type        string         `json:"type"`
	Status      string         `json:"status"`
	Image       sql.NullString `json:"image"`
	Command     sql.NullString `json:"command"`
	Environment sql.NullString `json:"environment"`
	Cpu         sql.NullString `json:"cpu"`
	Memory      sql.NullString `json:"memory"`
	CreatedAt   sql.NullTime   `json:"created_at"`
	UpdatedAt   sql.NullTime   `json:"updated_at"`
}

func (q *Queries) CreateServiceFromTemplate(ctx context.Context, arg CreateServiceFromTemplateParams) error {
	_, err := q.db.ExecContext(ctx, createServiceFromTemplate,
		arg.ID,
		arg.ProjectID,
		arg.Name,
		arg.Type,
		arg.Status,
		arg.Image,
		arg.Command,
		arg.Environment,
		arg.Cpu,
		arg.Memory,
		arg.CreatedAt,
		arg.UpdatedAt,
	)
	return err
}

const getProjectOwnerID = `-- name: GetProjectOwnerID :one
SELECT owner_id
FROM projects
WHERE id = $1
`

func (q *Queries) GetProjectOwnerID(ctx context.Context, id uuid.UUID) (uuid.UUID, error) {
	row := q.db.QueryRowContext(ctx, getProjectOwnerID, id)
	var owner_id uuid.UUID
	err := row.Scan(&owner_id)
	return owner_id, err
}

const getServiceTemplateByID = `-- name: GetServiceTemplateByID :one
SELECT id, name, description, category, logo, config, variables, is_official, created_at, updated_at
FROM service_templates
WHERE id = $1
`

func (q *Queries) GetServiceTemplateByID(ctx context.Context, id string) (ServiceTemplate, error) {
	row := q.db.QueryRowContext(ctx, getServiceTemplateByID, id)
	var i ServiceTemplate
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Description,
		&i.Category,
		&i.Logo,
		&i.Config,
		&i.Variables,
		&i.IsOfficial,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const listServiceTemplates = `-- name: ListServiceTemplates :many
SELECT id, name, description, category, logo, config, variables, is_official, created_at, updated_at
FROM service_templates
ORDER BY is_official DESC, name ASC
`

func (q *Queries) ListServiceTemplates(ctx context.Context) ([]ServiceTemplate, error) {
	rows, err := q.db.QueryContext(ctx, listServiceTemplates)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ServiceTemplate{}
	for rows.Next() {
		var i ServiceTemplate
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Description,
			&i.Category,
			&i.Logo,
			&i.Config,
			&i.Variables,
			&i.IsOfficial,
			&i.CreatedAt,
			&i.UpdatedAt,
		); 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 listServiceTemplatesByCategory = `-- name: ListServiceTemplatesByCategory :many
SELECT id, name, description, category, logo, config, variables, is_official, created_at, updated_at
FROM service_templates
WHERE category = $1
ORDER BY is_official DESC, name ASC
`

func (q *Queries) ListServiceTemplatesByCategory(ctx context.Context, category string) ([]ServiceTemplate, error) {
	rows, err := q.db.QueryContext(ctx, listServiceTemplatesByCategory, category)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ServiceTemplate{}
	for rows.Next() {
		var i ServiceTemplate
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Description,
			&i.Category,
			&i.Logo,
			&i.Config,
			&i.Variables,
			&i.IsOfficial,
			&i.CreatedAt,
			&i.UpdatedAt,
		); 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 upsertEnvironmentVariable = `-- name: UpsertEnvironmentVariable :exec
INSERT INTO environment_variables (id, service_id, key, value, is_secret, created_at, updated_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)
ON CONFLICT (service_id, key) DO UPDATE
SET value = EXCLUDED.value,
    is_secret = EXCLUDED.is_secret,
    updated_at = EXCLUDED.updated_at
`

type UpsertEnvironmentVariableParams struct {
	ID        uuid.UUID    `json:"id"`
	ServiceID uuid.UUID    `json:"service_id"`
	Key       string       `json:"key"`
	Value     string       `json:"value"`
	IsSecret  sql.NullBool `json:"is_secret"`
	CreatedAt sql.NullTime `json:"created_at"`
	UpdatedAt sql.NullTime `json:"updated_at"`
}

func (q *Queries) UpsertEnvironmentVariable(ctx context.Context, arg UpsertEnvironmentVariableParams) error {
	_, err := q.db.ExecContext(ctx, upsertEnvironmentVariable,
		arg.ID,
		arg.ServiceID,
		arg.Key,
		arg.Value,
		arg.IsSecret,
		arg.CreatedAt,
		arg.UpdatedAt,
	)
	return err
}
