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

package dbgen

import (
	"context"

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

const CreateBranch = `-- name: CreateBranch :one
INSERT INTO branches (
        name,
        location,
        wallet_id,
        branch_manager_id,
        company_id,
        is_self_owned
    )
VALUES ($1, $2, $3, $4, $5, $6)
RETURNING id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at
`

type CreateBranchParams struct {
	Name            string `json:"name"`
	Location        string `json:"location"`
	WalletID        int64  `json:"wallet_id"`
	BranchManagerID int64  `json:"branch_manager_id"`
	CompanyID       int64  `json:"company_id"`
	IsSelfOwned     bool   `json:"is_self_owned"`
}

func (q *Queries) CreateBranch(ctx context.Context, arg CreateBranchParams) (Branch, error) {
	row := q.db.QueryRow(ctx, CreateBranch,
		arg.Name,
		arg.Location,
		arg.WalletID,
		arg.BranchManagerID,
		arg.CompanyID,
		arg.IsSelfOwned,
	)
	var i Branch
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Location,
		&i.IsActive,
		&i.WalletID,
		&i.BranchManagerID,
		&i.CompanyID,
		&i.IsSelfOwned,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const CreateBranchCashier = `-- name: CreateBranchCashier :one
INSERT INTO branch_cashiers (user_id, branch_id)
VALUES ($1, $2)
RETURNING id, user_id, branch_id
`

type CreateBranchCashierParams struct {
	UserID   int64 `json:"user_id"`
	BranchID int64 `json:"branch_id"`
}

func (q *Queries) CreateBranchCashier(ctx context.Context, arg CreateBranchCashierParams) (BranchCashier, error) {
	row := q.db.QueryRow(ctx, CreateBranchCashier, arg.UserID, arg.BranchID)
	var i BranchCashier
	err := row.Scan(&i.ID, &i.UserID, &i.BranchID)
	return i, err
}

const CreateBranchOperation = `-- name: CreateBranchOperation :one
INSERT INTO branch_operations (operation_id, branch_id)
VALUES ($1, $2)
RETURNING id, operation_id, branch_id, created_at, updated_at
`

type CreateBranchOperationParams struct {
	OperationID int64 `json:"operation_id"`
	BranchID    int64 `json:"branch_id"`
}

func (q *Queries) CreateBranchOperation(ctx context.Context, arg CreateBranchOperationParams) (BranchOperation, error) {
	row := q.db.QueryRow(ctx, CreateBranchOperation, arg.OperationID, arg.BranchID)
	var i BranchOperation
	err := row.Scan(
		&i.ID,
		&i.OperationID,
		&i.BranchID,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const CreateSupportedOperation = `-- name: CreateSupportedOperation :one
INSERT INTO supported_operations (name, description)
VALUES ($1, $2)
RETURNING id, name, description
`

type CreateSupportedOperationParams struct {
	Name        string `json:"name"`
	Description string `json:"description"`
}

func (q *Queries) CreateSupportedOperation(ctx context.Context, arg CreateSupportedOperationParams) (SupportedOperation, error) {
	row := q.db.QueryRow(ctx, CreateSupportedOperation, arg.Name, arg.Description)
	var i SupportedOperation
	err := row.Scan(&i.ID, &i.Name, &i.Description)
	return i, err
}

const DeleteBranch = `-- name: DeleteBranch :exec
DELETE FROM branches
WHERE id = $1
`

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

const DeleteBranchCashier = `-- name: DeleteBranchCashier :exec
DELETE FROM branch_cashiers
WHERE user_id = $1
`

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

const DeleteBranchOperation = `-- name: DeleteBranchOperation :exec
DELETE FROM branch_operations
WHERE operation_id = $1
    AND branch_id = $2
`

type DeleteBranchOperationParams struct {
	OperationID int64 `json:"operation_id"`
	BranchID    int64 `json:"branch_id"`
}

func (q *Queries) DeleteBranchOperation(ctx context.Context, arg DeleteBranchOperationParams) error {
	_, err := q.db.Exec(ctx, DeleteBranchOperation, arg.OperationID, arg.BranchID)
	return err
}

const GetAllBranches = `-- name: GetAllBranches :many
SELECT id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance
FROM branch_details
`

func (q *Queries) GetAllBranches(ctx context.Context) ([]BranchDetail, error) {
	rows, err := q.db.Query(ctx, GetAllBranches)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BranchDetail
	for rows.Next() {
		var i BranchDetail
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Location,
			&i.IsActive,
			&i.WalletID,
			&i.BranchManagerID,
			&i.CompanyID,
			&i.IsSelfOwned,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.ManagerName,
			&i.ManagerPhoneNumber,
			&i.Balance,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetAllSupportedOperations = `-- name: GetAllSupportedOperations :many
SELECT id, name, description
FROM supported_operations
`

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

const GetBranchByCashier = `-- name: GetBranchByCashier :one
SELECT branches.id, branches.name, branches.location, branches.is_active, branches.wallet_id, branches.branch_manager_id, branches.company_id, branches.is_self_owned, branches.created_at, branches.updated_at
FROM branch_cashiers
    JOIN branches ON branch_cashiers.branch_id = branches.id
WHERE branch_cashiers.user_id = $1
`

func (q *Queries) GetBranchByCashier(ctx context.Context, userID int64) (Branch, error) {
	row := q.db.QueryRow(ctx, GetBranchByCashier, userID)
	var i Branch
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Location,
		&i.IsActive,
		&i.WalletID,
		&i.BranchManagerID,
		&i.CompanyID,
		&i.IsSelfOwned,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

const GetBranchByCompanyID = `-- name: GetBranchByCompanyID :many
SELECT id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance
FROM branch_details
WHERE company_id = $1
`

func (q *Queries) GetBranchByCompanyID(ctx context.Context, companyID int64) ([]BranchDetail, error) {
	rows, err := q.db.Query(ctx, GetBranchByCompanyID, companyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BranchDetail
	for rows.Next() {
		var i BranchDetail
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Location,
			&i.IsActive,
			&i.WalletID,
			&i.BranchManagerID,
			&i.CompanyID,
			&i.IsSelfOwned,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.ManagerName,
			&i.ManagerPhoneNumber,
			&i.Balance,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetBranchByID = `-- name: GetBranchByID :one
SELECT id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance
FROM branch_details
WHERE id = $1
`

func (q *Queries) GetBranchByID(ctx context.Context, id int64) (BranchDetail, error) {
	row := q.db.QueryRow(ctx, GetBranchByID, id)
	var i BranchDetail
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Location,
		&i.IsActive,
		&i.WalletID,
		&i.BranchManagerID,
		&i.CompanyID,
		&i.IsSelfOwned,
		&i.CreatedAt,
		&i.UpdatedAt,
		&i.ManagerName,
		&i.ManagerPhoneNumber,
		&i.Balance,
	)
	return i, err
}

const GetBranchByManagerID = `-- name: GetBranchByManagerID :many
SELECT id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance
FROM branch_details
WHERE branch_manager_id = $1
`

func (q *Queries) GetBranchByManagerID(ctx context.Context, branchManagerID int64) ([]BranchDetail, error) {
	rows, err := q.db.Query(ctx, GetBranchByManagerID, branchManagerID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BranchDetail
	for rows.Next() {
		var i BranchDetail
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Location,
			&i.IsActive,
			&i.WalletID,
			&i.BranchManagerID,
			&i.CompanyID,
			&i.IsSelfOwned,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.ManagerName,
			&i.ManagerPhoneNumber,
			&i.Balance,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const GetBranchOperations = `-- name: GetBranchOperations :many
SELECT branch_operations.id, branch_operations.operation_id, branch_operations.branch_id, branch_operations.created_at, branch_operations.updated_at,
    supported_operations.name,
    supported_operations.description
FROM branch_operations
    JOIN supported_operations ON branch_operations.operation_id = supported_operations.id
WHERE branch_operations.branch_id = $1
`

type GetBranchOperationsRow struct {
	ID          int64            `json:"id"`
	OperationID int64            `json:"operation_id"`
	BranchID    int64            `json:"branch_id"`
	CreatedAt   pgtype.Timestamp `json:"created_at"`
	UpdatedAt   pgtype.Timestamp `json:"updated_at"`
	Name        string           `json:"name"`
	Description string           `json:"description"`
}

func (q *Queries) GetBranchOperations(ctx context.Context, branchID int64) ([]GetBranchOperationsRow, error) {
	rows, err := q.db.Query(ctx, GetBranchOperations, branchID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []GetBranchOperationsRow
	for rows.Next() {
		var i GetBranchOperationsRow
		if err := rows.Scan(
			&i.ID,
			&i.OperationID,
			&i.BranchID,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.Name,
			&i.Description,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const SearchBranchByName = `-- name: SearchBranchByName :many
SELECT id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at, manager_name, manager_phone_number, balance
FROM branch_details
WHERE name ILIKE '%' || $1 || '%'
`

func (q *Queries) SearchBranchByName(ctx context.Context, dollar_1 pgtype.Text) ([]BranchDetail, error) {
	rows, err := q.db.Query(ctx, SearchBranchByName, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	var items []BranchDetail
	for rows.Next() {
		var i BranchDetail
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Location,
			&i.IsActive,
			&i.WalletID,
			&i.BranchManagerID,
			&i.CompanyID,
			&i.IsSelfOwned,
			&i.CreatedAt,
			&i.UpdatedAt,
			&i.ManagerName,
			&i.ManagerPhoneNumber,
			&i.Balance,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const UpdateBranch = `-- name: UpdateBranch :one
UPDATE branches
SET name = COALESCE($2, name),
    location = COALESCE($3, location),
    branch_manager_id = COALESCE($4, branch_manager_id),
    company_id = COALESCE($5, company_id),
    is_self_owned = COALESCE($6, is_self_owned),
    is_active = COALESCE($7, is_active)
WHERE id = $1
RETURNING id, name, location, is_active, wallet_id, branch_manager_id, company_id, is_self_owned, created_at, updated_at
`

type UpdateBranchParams struct {
	ID              int64       `json:"id"`
	Name            pgtype.Text `json:"name"`
	Location        pgtype.Text `json:"location"`
	BranchManagerID pgtype.Int8 `json:"branch_manager_id"`
	CompanyID       pgtype.Int8 `json:"company_id"`
	IsSelfOwned     pgtype.Bool `json:"is_self_owned"`
	IsActive        pgtype.Bool `json:"is_active"`
}

func (q *Queries) UpdateBranch(ctx context.Context, arg UpdateBranchParams) (Branch, error) {
	row := q.db.QueryRow(ctx, UpdateBranch,
		arg.ID,
		arg.Name,
		arg.Location,
		arg.BranchManagerID,
		arg.CompanyID,
		arg.IsSelfOwned,
		arg.IsActive,
	)
	var i Branch
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Location,
		&i.IsActive,
		&i.WalletID,
		&i.BranchManagerID,
		&i.CompanyID,
		&i.IsSelfOwned,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}
