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

package db

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

const allVillages = `-- name: AllVillages :many
SELECT parishes.parish, parishes.code,districts.name district_name,districts.code district_code,
village,villages.created_at,villages.id,districts.id as district_id,parishes.id as parish_id,
subcounties.id as subcounty_id, villages.id as villages_id FROM villages
JOIN parishes    on parishes.id = villages.parish_id
JOIN subcounties on subcounties.id = parishes.subcounty_id
JOIN districts   on districts.id = subcounties.district_id
`

type AllVillagesRow struct {
	Parish       string    `json:"parish"`
	Code         string    `json:"code"`
	DistrictName string    `json:"district_name"`
	DistrictCode string    `json:"district_code"`
	Village      string    `json:"village"`
	CreatedAt    time.Time `json:"created_at"`
	ID           int64     `json:"id"`
	DistrictID   int64     `json:"district_id"`
	ParishID     int64     `json:"parish_id"`
	SubcountyID  int64     `json:"subcounty_id"`
	VillagesID   int64     `json:"villages_id"`
}

func (q *Queries) AllVillages(ctx context.Context) ([]AllVillagesRow, error) {
	rows, err := q.db.QueryContext(ctx, allVillages)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []AllVillagesRow{}
	for rows.Next() {
		var i AllVillagesRow
		if err := rows.Scan(
			&i.Parish,
			&i.Code,
			&i.DistrictName,
			&i.DistrictCode,
			&i.Village,
			&i.CreatedAt,
			&i.ID,
			&i.DistrictID,
			&i.ParishID,
			&i.SubcountyID,
			&i.VillagesID,
		); 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 allVillagesForDistrict = `-- name: AllVillagesForDistrict :many
SELECT id, village, code, parish_id, created_at FROM villages
WHERE parish_id = $1
`

func (q *Queries) AllVillagesForDistrict(ctx context.Context, parishID int32) ([]Village, error) {
	rows, err := q.db.QueryContext(ctx, allVillagesForDistrict, parishID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Village{}
	for rows.Next() {
		var i Village
		if err := rows.Scan(
			&i.ID,
			&i.Village,
			&i.Code,
			&i.ParishID,
			&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 createVillage = `-- name: CreateVillage :one
INSERT INTO villages (
     village,
     parish_id,
     code
 ) VALUES (
    $1, $2, $3
)
RETURNING id, village, code, parish_id, created_at
`

type CreateVillageParams struct {
	Village  string `json:"village"`
	ParishID int32  `json:"parish_id"`
	Code     string `json:"code"`
}

func (q *Queries) CreateVillage(ctx context.Context, arg CreateVillageParams) (Village, error) {
	row := q.db.QueryRowContext(ctx, createVillage, arg.Village, arg.ParishID, arg.Code)
	var i Village
	err := row.Scan(
		&i.ID,
		&i.Village,
		&i.Code,
		&i.ParishID,
		&i.CreatedAt,
	)
	return i, err
}

const getVillage = `-- name: GetVillage :one
SELECT id, village, code, parish_id, created_at FROM villages
WHERE id = $1 LIMIT 1
`

func (q *Queries) GetVillage(ctx context.Context, id int64) (Village, error) {
	row := q.db.QueryRowContext(ctx, getVillage, id)
	var i Village
	err := row.Scan(
		&i.ID,
		&i.Village,
		&i.Code,
		&i.ParishID,
		&i.CreatedAt,
	)
	return i, err
}

const getVillageIdByName = `-- name: GetVillageIdByName :one
SELECT id FROM villages
WHERE village = $1
`

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

const listVillages = `-- name: ListVillages :many
SELECT village,parishes.code,parishes.parish,
villages.id as village_id,parishes.id as parish_id,
subcounties.id as subcounty_id,districts.id as district_id FROM villages
JOIN   parishes    ON parishes.id    = villages.parish_id
JOIN   subcounties ON subcounties.id = parishes.subcounty_id
JOIN   districts   ON districts.id   = subcounties.district_id
LIMIT  $1
OFFSET $2
`

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

type ListVillagesRow struct {
	Village     string `json:"village"`
	Code        string `json:"code"`
	Parish      string `json:"parish"`
	VillageID   int64  `json:"village_id"`
	ParishID    int64  `json:"parish_id"`
	SubcountyID int64  `json:"subcounty_id"`
	DistrictID  int64  `json:"district_id"`
}

func (q *Queries) ListVillages(ctx context.Context, arg ListVillagesParams) ([]ListVillagesRow, error) {
	rows, err := q.db.QueryContext(ctx, listVillages, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListVillagesRow{}
	for rows.Next() {
		var i ListVillagesRow
		if err := rows.Scan(
			&i.Village,
			&i.Code,
			&i.Parish,
			&i.VillageID,
			&i.ParishID,
			&i.SubcountyID,
			&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 updateVillage = `-- name: UpdateVillage :one
UPDATE villages
  set
     village      = COALESCE($1, village),
     parish_id    = COALESCE($2, parish_id),
     code         = COALESCE($3, code)
WHERE id = $4
RETURNING id, village, code, parish_id, created_at
`

type UpdateVillageParams struct {
	Village  sql.NullString `json:"village"`
	ParishID sql.NullInt32  `json:"parish_id"`
	Code     sql.NullString `json:"code"`
	ID       int64          `json:"id"`
}

func (q *Queries) UpdateVillage(ctx context.Context, arg UpdateVillageParams) (Village, error) {
	row := q.db.QueryRowContext(ctx, updateVillage,
		arg.Village,
		arg.ParishID,
		arg.Code,
		arg.ID,
	)
	var i Village
	err := row.Scan(
		&i.ID,
		&i.Village,
		&i.Code,
		&i.ParishID,
		&i.CreatedAt,
	)
	return i, err
}
