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

package db

import (
	"context"
	"database/sql"
)

const checkIfNINExists = `-- name: CheckIfNINExists :one
SELECT id, name, nin, exihandler_id, created_at, updated_at
FROM clearing_agents
WHERE nin = $1
`

func (q *Queries) CheckIfNINExists(ctx context.Context, nin string) (ClearingAgent, error) {
	row := q.db.QueryRowContext(ctx, checkIfNINExists, nin)
	var i ClearingAgent
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Nin,
		&i.ExihandlerID,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const createClearingAgent = `-- name: CreateClearingAgent :one
INSERT INTO clearing_agents (name, nin, exihandler_id)
VALUES ($1, $2, $3)
RETURNING id, name, nin, exihandler_id, created_at, updated_at
`

type CreateClearingAgentParams struct {
	Name         string `json:"name"`
	Nin          string `json:"nin"`
	ExihandlerID int32  `json:"exihandler_id"`
}

func (q *Queries) CreateClearingAgent(ctx context.Context, arg CreateClearingAgentParams) (ClearingAgent, error) {
	row := q.db.QueryRowContext(ctx, createClearingAgent, arg.Name, arg.Nin, arg.ExihandlerID)
	var i ClearingAgent
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Nin,
		&i.ExihandlerID,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const deleteClearingAgent = `-- name: DeleteClearingAgent :exec
DELETE FROM clearing_agents
WHERE id = $1
`

func (q *Queries) DeleteClearingAgent(ctx context.Context, id int32) error {
	_, err := q.db.ExecContext(ctx, deleteClearingAgent, id)
	return err
}

const getAllClearingAgents = `-- name: GetAllClearingAgents :many
SELECT clearing_agents.id, clearing_agents.name, clearing_agents.nin, clearing_agents.exihandler_id, 
clearing_agents.created_at, clearing_agents.updated_at, 
exithandlers.exhcode, exithandlers.exhdescription
FROM clearing_agents
JOIN exithandlers on exithandlers.id = clearing_agents.exihandler_id
`

type GetAllClearingAgentsRow struct {
	ID             int32        `json:"id"`
	Name           string       `json:"name"`
	Nin            string       `json:"nin"`
	ExihandlerID   int32        `json:"exihandler_id"`
	CreatedAt      sql.NullTime `json:"created_at"`
	UpdatedAt      sql.NullTime `json:"updated_at"`
	Exhcode        string       `json:"exhcode"`
	Exhdescription string       `json:"exhdescription"`
}

func (q *Queries) GetAllClearingAgents(ctx context.Context) ([]GetAllClearingAgentsRow, error) {
	rows, err := q.db.QueryContext(ctx, getAllClearingAgents)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetAllClearingAgentsRow{}
	for rows.Next() {
		var i GetAllClearingAgentsRow
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Nin,
			&i.ExihandlerID,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.Exhcode,
			&i.Exhdescription,
		); 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 getClearingAgentByID = `-- name: GetClearingAgentByID :one
SELECT id, name, nin, exihandler_id, created_at, updated_at
FROM clearing_agents
WHERE id = $1
`

func (q *Queries) GetClearingAgentByID(ctx context.Context, id int32) (ClearingAgent, error) {
	row := q.db.QueryRowContext(ctx, getClearingAgentByID, id)
	var i ClearingAgent
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Nin,
		&i.ExihandlerID,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const getClearingAgentsByExihandlerID = `-- name: GetClearingAgentsByExihandlerID :many
SELECT id, name, nin, exihandler_id, created_at, updated_at
FROM clearing_agents
WHERE exihandler_id = $1
`

func (q *Queries) GetClearingAgentsByExihandlerID(ctx context.Context, exihandlerID int32) ([]ClearingAgent, error) {
	rows, err := q.db.QueryContext(ctx, getClearingAgentsByExihandlerID, exihandlerID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ClearingAgent{}
	for rows.Next() {
		var i ClearingAgent
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Nin,
			&i.ExihandlerID,
			&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 updateClearingAgent = `-- name: UpdateClearingAgent :one
UPDATE clearing_agents
SET name = $1, nin = $2, exihandler_id = $3, updated_at = NOW()
WHERE id = $4
RETURNING id, name, nin, exihandler_id, created_at, updated_at
`

type UpdateClearingAgentParams struct {
	Name         string `json:"name"`
	Nin          string `json:"nin"`
	ExihandlerID int32  `json:"exihandler_id"`
	ID           int32  `json:"id"`
}

func (q *Queries) UpdateClearingAgent(ctx context.Context, arg UpdateClearingAgentParams) (ClearingAgent, error) {
	row := q.db.QueryRowContext(ctx, updateClearingAgent,
		arg.Name,
		arg.Nin,
		arg.ExihandlerID,
		arg.ID,
	)
	var i ClearingAgent
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Nin,
		&i.ExihandlerID,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
