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

package repository

import (
	"context"

	"github.com/google/uuid"
)

const activateApiService = `-- name: ActivateApiService :exec
UPDATE api_services
SET is_active = true,
    updated_at = NOW()
WHERE client_id = $1
`

func (q *Queries) ActivateApiService(ctx context.Context, clientID string) error {
	_, err := q.db.Exec(ctx, activateApiService, clientID)
	return err
}

const createApiService = `-- name: CreateApiService :one
INSERT INTO api_services (
    client_id, client_secret, name, description, redirect_uris, scopes, grant_types, is_active
) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8
) RETURNING id, client_id, client_secret, name, redirect_uris, scopes, grant_types, created_at, updated_at, is_active, description
`

type CreateApiServiceParams struct {
	ClientID     string   `json:"client_id"`
	ClientSecret string   `json:"client_secret"`
	Name         string   `json:"name"`
	Description  *string  `json:"description"`
	RedirectUris []string `json:"redirect_uris"`
	Scopes       []string `json:"scopes"`
	GrantTypes   []string `json:"grant_types"`
	IsActive     bool     `json:"is_active"`
}

func (q *Queries) CreateApiService(ctx context.Context, arg CreateApiServiceParams) (ApiService, error) {
	row := q.db.QueryRow(ctx, createApiService,
		arg.ClientID,
		arg.ClientSecret,
		arg.Name,
		arg.Description,
		arg.RedirectUris,
		arg.Scopes,
		arg.GrantTypes,
		arg.IsActive,
	)
	var i ApiService
	err := row.Scan(
		&i.ID,
		&i.ClientID,
		&i.ClientSecret,
		&i.Name,
		&i.RedirectUris,
		&i.Scopes,
		&i.GrantTypes,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.IsActive,
		&i.Description,
	)
	return i, err
}

const deactivateApiService = `-- name: DeactivateApiService :exec
UPDATE api_services
SET is_active = false,
    updated_at = NOW()
WHERE client_id = $1
`

func (q *Queries) DeactivateApiService(ctx context.Context, clientID string) error {
	_, err := q.db.Exec(ctx, deactivateApiService, clientID)
	return err
}

const getApiServiceCID = `-- name: GetApiServiceCID :one
SELECT id, client_id, client_secret, name, redirect_uris, scopes, grant_types, created_at, updated_at, is_active, description FROM api_services
WHERE client_id = $1
  AND is_active = true
LIMIT 1
`

func (q *Queries) GetApiServiceCID(ctx context.Context, clientID string) (ApiService, error) {
	row := q.db.QueryRow(ctx, getApiServiceCID, clientID)
	var i ApiService
	err := row.Scan(
		&i.ID,
		&i.ClientID,
		&i.ClientSecret,
		&i.Name,
		&i.RedirectUris,
		&i.Scopes,
		&i.GrantTypes,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.IsActive,
		&i.Description,
	)
	return i, err
}

const getApiServiceId = `-- name: GetApiServiceId :one
SELECT id, client_id, client_secret, name, redirect_uris, scopes, grant_types, created_at, updated_at, is_active, description FROM api_services
WHERE id = $1
LIMIT 1
`

func (q *Queries) GetApiServiceId(ctx context.Context, id uuid.UUID) (ApiService, error) {
	row := q.db.QueryRow(ctx, getApiServiceId, id)
	var i ApiService
	err := row.Scan(
		&i.ID,
		&i.ClientID,
		&i.ClientSecret,
		&i.Name,
		&i.RedirectUris,
		&i.Scopes,
		&i.GrantTypes,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.IsActive,
		&i.Description,
	)
	return i, err
}

const listApiServices = `-- name: ListApiServices :many
SELECT id, client_id, client_secret, name, redirect_uris, scopes, grant_types, created_at, updated_at, is_active, description FROM api_services
ORDER BY created_at DESC
`

func (q *Queries) ListApiServices(ctx context.Context) ([]ApiService, error) {
	rows, err := q.db.Query(ctx, listApiServices)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []ApiService
	for rows.Next() {
		var i ApiService
		if err := rows.Scan(
			&i.ID,
			&i.ClientID,
			&i.ClientSecret,
			&i.Name,
			&i.RedirectUris,
			&i.Scopes,
			&i.GrantTypes,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.IsActive,
			&i.Description,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateApiService = `-- name: UpdateApiService :one
UPDATE api_services
SET
    name = $2,
    description = $3,
    redirect_uris = $4,
    scopes = $5,
    grant_types = $6,
    updated_at = NOW()
WHERE client_id = $1
RETURNING id, client_id, client_secret, name, redirect_uris, scopes, grant_types, created_at, updated_at, is_active, description
`

type UpdateApiServiceParams struct {
	ClientID     string   `json:"client_id"`
	Name         string   `json:"name"`
	Description  *string  `json:"description"`
	RedirectUris []string `json:"redirect_uris"`
	Scopes       []string `json:"scopes"`
	GrantTypes   []string `json:"grant_types"`
}

func (q *Queries) UpdateApiService(ctx context.Context, arg UpdateApiServiceParams) (ApiService, error) {
	row := q.db.QueryRow(ctx, updateApiService,
		arg.ClientID,
		arg.Name,
		arg.Description,
		arg.RedirectUris,
		arg.Scopes,
		arg.GrantTypes,
	)
	var i ApiService
	err := row.Scan(
		&i.ID,
		&i.ClientID,
		&i.ClientSecret,
		&i.Name,
		&i.RedirectUris,
		&i.Scopes,
		&i.GrantTypes,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.IsActive,
		&i.Description,
	)
	return i, err
}

const updateClientSecret = `-- name: UpdateClientSecret :exec
UPDATE api_services
SET client_secret = $2,
    updated_at = NOW()
WHERE client_id = $1
`

type UpdateClientSecretParams struct {
	ClientID     string `json:"client_id"`
	ClientSecret string `json:"client_secret"`
}

func (q *Queries) UpdateClientSecret(ctx context.Context, arg UpdateClientSecretParams) error {
	_, err := q.db.Exec(ctx, updateClientSecret, arg.ClientID, arg.ClientSecret)
	return err
}
