// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.28.0
// source: parishes.sql

package db

import (
	"context"
	"database/sql"
	"time"
)

const allParishes = `-- name: AllParishes :many
SELECT parish,subcounties.code subcounty_code,
subcounties.name subcounty_name,
subcounties.id subcounty_id,
parishes.id parish_id,
parishes.code parish_code, 
parishes.created_at,districts.name, districts.id as district_id FROM parishes
JOIN  subcounties ON subcounties.id = parishes.subcounty_id
JOIN  districts   on districts.id   = subcounties.district_id
`

type AllParishesRow struct {
	Parish        string    `json:"parish"`
	SubcountyCode string    `json:"subcounty_code"`
	SubcountyName string    `json:"subcounty_name"`
	SubcountyID   int64     `json:"subcounty_id"`
	ParishID      int64     `json:"parish_id"`
	ParishCode    string    `json:"parish_code"`
	CreatedAt     time.Time `json:"created_at"`
	Name          string    `json:"name"`
	DistrictID    int64     `json:"district_id"`
}

func (q *Queries) AllParishes(ctx context.Context) ([]AllParishesRow, error) {
	rows, err := q.db.QueryContext(ctx, allParishes)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []AllParishesRow{}
	for rows.Next() {
		var i AllParishesRow
		if err := rows.Scan(
			&i.Parish,
			&i.SubcountyCode,
			&i.SubcountyName,
			&i.SubcountyID,
			&i.ParishID,
			&i.ParishCode,
			&i.CreatedAt,
			&i.Name,
			&i.DistrictID,
		); 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 allParishesForDistrict = `-- name: AllParishesForDistrict :many
SELECT id, parish, subcounty_id, code, created_at FROM parishes
WHERE subcounty_id = $1
`

func (q *Queries) AllParishesForDistrict(ctx context.Context, subcountyID int32) ([]Parish, error) {
	rows, err := q.db.QueryContext(ctx, allParishesForDistrict, subcountyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Parish{}
	for rows.Next() {
		var i Parish
		if err := rows.Scan(
			&i.ID,
			&i.Parish,
			&i.SubcountyID,
			&i.Code,
			&i.CreatedAt,
		); 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 createParish = `-- name: CreateParish :one
INSERT INTO parishes (           
     parish,
     subcounty_id,
     code
 ) VALUES (
    $1, $2, $3
) 
RETURNING id, parish, subcounty_id, code, created_at
`

type CreateParishParams struct {
	Parish      string `json:"parish"`
	SubcountyID int32  `json:"subcounty_id"`
	Code        string `json:"code"`
}

func (q *Queries) CreateParish(ctx context.Context, arg CreateParishParams) (Parish, error) {
	row := q.db.QueryRowContext(ctx, createParish, arg.Parish, arg.SubcountyID, arg.Code)
	var i Parish
	err := row.Scan(
		&i.ID,
		&i.Parish,
		&i.SubcountyID,
		&i.Code,
		&i.CreatedAt,
	)
	return i, err
}

const getAdminUnits = `-- name: GetAdminUnits :many
SELECT parishes.parish parish_name,subcounties.code subcounty_code,
subcounties.name subcounty_name,subcounties.id subcounty_id,
parishes.id parish_id,districts.name district_name,
districts.id district_id,parishes.id,parishes.code parish_code,
districts.created_at,villages.village,villages.id village_id FROM districts
LEFT JOIN  subcounties   on subcounties.district_id    =  districts.id
LEFT JOIN  parishes      on parishes.subcounty_id      = subcounties.id
LEFT JOIN  villages      on villages.parish_id         = parishes.id
LIMIT $1 OFFSET $2
`

type GetAdminUnitsParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

type GetAdminUnitsRow struct {
	ParishName    sql.NullString `json:"parish_name"`
	SubcountyCode sql.NullString `json:"subcounty_code"`
	SubcountyName sql.NullString `json:"subcounty_name"`
	SubcountyID   sql.NullInt64  `json:"subcounty_id"`
	ParishID      sql.NullInt64  `json:"parish_id"`
	DistrictName  string         `json:"district_name"`
	DistrictID    int64          `json:"district_id"`
	ID            sql.NullInt64  `json:"id"`
	ParishCode    sql.NullString `json:"parish_code"`
	CreatedAt     time.Time      `json:"created_at"`
	Village       sql.NullString `json:"village"`
	VillageID     sql.NullInt64  `json:"village_id"`
}

func (q *Queries) GetAdminUnits(ctx context.Context, arg GetAdminUnitsParams) ([]GetAdminUnitsRow, error) {
	rows, err := q.db.QueryContext(ctx, getAdminUnits, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetAdminUnitsRow{}
	for rows.Next() {
		var i GetAdminUnitsRow
		if err := rows.Scan(
			&i.ParishName,
			&i.SubcountyCode,
			&i.SubcountyName,
			&i.SubcountyID,
			&i.ParishID,
			&i.DistrictName,
			&i.DistrictID,
			&i.ID,
			&i.ParishCode,
			&i.CreatedAt,
			&i.Village,
			&i.VillageID,
		); 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 getParish = `-- name: GetParish :one
SELECT id, parish, subcounty_id, code, created_at FROM parishes
WHERE id = $1 LIMIT 1
`

func (q *Queries) GetParish(ctx context.Context, id int64) (Parish, error) {
	row := q.db.QueryRowContext(ctx, getParish, id)
	var i Parish
	err := row.Scan(
		&i.ID,
		&i.Parish,
		&i.SubcountyID,
		&i.Code,
		&i.CreatedAt,
	)
	return i, err
}

const getParishByName = `-- name: GetParishByName :one
SELECT id, parish, subcounty_id, code, created_at FROM parishes
WHERE parish = $1 LIMIT 1
`

func (q *Queries) GetParishByName(ctx context.Context, parish string) (Parish, error) {
	row := q.db.QueryRowContext(ctx, getParishByName, parish)
	var i Parish
	err := row.Scan(
		&i.ID,
		&i.Parish,
		&i.SubcountyID,
		&i.Code,
		&i.CreatedAt,
	)
	return i, err
}

const getParishBySubCounty = `-- name: GetParishBySubCounty :many
SELECT id, parish, subcounty_id, code, created_at FROM parishes
WHERE subcounty_id = $1
`

func (q *Queries) GetParishBySubCounty(ctx context.Context, subcountyID int32) ([]Parish, error) {
	rows, err := q.db.QueryContext(ctx, getParishBySubCounty, subcountyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Parish{}
	for rows.Next() {
		var i Parish
		if err := rows.Scan(
			&i.ID,
			&i.Parish,
			&i.SubcountyID,
			&i.Code,
			&i.CreatedAt,
		); 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 getParishIdByName = `-- name: GetParishIdByName :one
SELECT id FROM parishes
WHERE parish = $1 LIMIT 1
`

func (q *Queries) GetParishIdByName(ctx context.Context, parish string) (int64, error) {
	row := q.db.QueryRowContext(ctx, getParishIdByName, parish)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const listAdminUnits = `-- name: ListAdminUnits :many
SELECT parishes.parish parish_name,subcounties.code subcounty_code,
subcounties.name subcounty_name,subcounties.id subcounty_id,
parishes.id parish_id,districts.name district_name,
districts.id district_id,parishes.id,parishes.code parish_code,
districts.created_at,villages.village,villages.id village_id FROM districts
LEFT JOIN  subcounties   on subcounties.district_id    =  districts.id
LEFT JOIN  parishes      on parishes.subcounty_id      = subcounties.id
LEFT JOIN  villages      on villages.parish_id         = parishes.id
`

type ListAdminUnitsRow struct {
	ParishName    sql.NullString `json:"parish_name"`
	SubcountyCode sql.NullString `json:"subcounty_code"`
	SubcountyName sql.NullString `json:"subcounty_name"`
	SubcountyID   sql.NullInt64  `json:"subcounty_id"`
	ParishID      sql.NullInt64  `json:"parish_id"`
	DistrictName  string         `json:"district_name"`
	DistrictID    int64          `json:"district_id"`
	ID            sql.NullInt64  `json:"id"`
	ParishCode    sql.NullString `json:"parish_code"`
	CreatedAt     time.Time      `json:"created_at"`
	Village       sql.NullString `json:"village"`
	VillageID     sql.NullInt64  `json:"village_id"`
}

func (q *Queries) ListAdminUnits(ctx context.Context) ([]ListAdminUnitsRow, error) {
	rows, err := q.db.QueryContext(ctx, listAdminUnits)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListAdminUnitsRow{}
	for rows.Next() {
		var i ListAdminUnitsRow
		if err := rows.Scan(
			&i.ParishName,
			&i.SubcountyCode,
			&i.SubcountyName,
			&i.SubcountyID,
			&i.ParishID,
			&i.DistrictName,
			&i.DistrictID,
			&i.ID,
			&i.ParishCode,
			&i.CreatedAt,
			&i.Village,
			&i.VillageID,
		); 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 listParishes = `-- name: ListParishes :many
SELECT parishes.parish,districts.code,districts.id as district_id, 
subcounties.id as subcounty_id,subcounties.name as subcounty_name,
districts.name, parishes.id as parish_id,subcounties.id as subcounty_id FROM parishes
JOIN   subcounties ON subcounties.id = parishes.subcounty_id
JOIN   districts   ON districts.id   = subcounties.district_id
LIMIT  $1
OFFSET $2
`

type ListParishesParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

type ListParishesRow struct {
	Parish        string `json:"parish"`
	Code          string `json:"code"`
	DistrictID    int64  `json:"district_id"`
	SubcountyID   int64  `json:"subcounty_id"`
	SubcountyName string `json:"subcounty_name"`
	Name          string `json:"name"`
	ParishID      int64  `json:"parish_id"`
	SubcountyID_2 int64  `json:"subcounty_id_2"`
}

func (q *Queries) ListParishes(ctx context.Context, arg ListParishesParams) ([]ListParishesRow, error) {
	rows, err := q.db.QueryContext(ctx, listParishes, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListParishesRow{}
	for rows.Next() {
		var i ListParishesRow
		if err := rows.Scan(
			&i.Parish,
			&i.Code,
			&i.DistrictID,
			&i.SubcountyID,
			&i.SubcountyName,
			&i.Name,
			&i.ParishID,
			&i.SubcountyID_2,
		); 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 updateParish = `-- name: UpdateParish :one
UPDATE parishes
  set 
     parish      = COALESCE($1, parish),
     code        = COALESCE($2, code),
     subcounty_id = COALESCE($3, subcounty_id)
WHERE id = $4
RETURNING id, parish, subcounty_id, code, created_at
`

type UpdateParishParams struct {
	Parish      sql.NullString `json:"parish"`
	Code        sql.NullString `json:"code"`
	SubcountyID sql.NullInt32  `json:"subcounty_id"`
	ID          int64          `json:"id"`
}

func (q *Queries) UpdateParish(ctx context.Context, arg UpdateParishParams) (Parish, error) {
	row := q.db.QueryRowContext(ctx, updateParish,
		arg.Parish,
		arg.Code,
		arg.SubcountyID,
		arg.ID,
	)
	var i Parish
	err := row.Scan(
		&i.ID,
		&i.Parish,
		&i.SubcountyID,
		&i.Code,
		&i.CreatedAt,
	)
	return i, err
}
