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

package db

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

const allPackhouses = `-- name: AllPackhouses :many
SELECT id, phname, phregno, contactname, contactphone, contactemail, streetaddress, district_id, parish_id, village_id, long, lat, regstatus, expirydate, created_at FROM packhouses
`

func (q *Queries) AllPackhouses(ctx context.Context) ([]Packhouse, error) {
	rows, err := q.db.QueryContext(ctx, allPackhouses)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Packhouse{}
	for rows.Next() {
		var i Packhouse
		if err := rows.Scan(
			&i.ID,
			&i.Phname,
			&i.Phregno,
			&i.Contactname,
			&i.Contactphone,
			&i.Contactemail,
			&i.Streetaddress,
			&i.DistrictID,
			&i.ParishID,
			&i.VillageID,
			&i.Long,
			&i.Lat,
			&i.Regstatus,
			&i.Expirydate,
			&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 attachExporterPackhouse = `-- name: AttachExporterPackhouse :one
INSERT INTO packhouse_exporter (
   exportcompany_id,
   packhouse_id
)VALUES (
  $1,
  $2
 ) RETURNING id, packhouse_id, exportcompany_id, created_at
`

type AttachExporterPackhouseParams struct {
	ExportcompanyID int32 `json:"exportcompany_id"`
	PackhouseID     int32 `json:"packhouse_id"`
}

func (q *Queries) AttachExporterPackhouse(ctx context.Context, arg AttachExporterPackhouseParams) (PackhouseExporter, error) {
	row := q.db.QueryRowContext(ctx, attachExporterPackhouse, arg.ExportcompanyID, arg.PackhouseID)
	var i PackhouseExporter
	err := row.Scan(
		&i.ID,
		&i.PackhouseID,
		&i.ExportcompanyID,
		&i.CreatedAt,
	)
	return i, err
}

const createPackHouse = `-- name: CreatePackHouse :one
INSERT INTO packhouses (
    phname,
    phregno,
    contactname,
    contactphone,
    contactemail,
    streetaddress,
    district_id,
    long,
    lat,
    expirydate,
    regstatus,
    parish_id,
    village_id
 ) VALUES (
      $1,$2,$3,$4,$5, $6,$7,$8,$9,$10,$11,$12,$13
 ) RETURNING id, phname, phregno, contactname, contactphone, contactemail, streetaddress, district_id, parish_id, village_id, long, lat, regstatus, expirydate, created_at
`

type CreatePackHouseParams struct {
	Phname        string    `json:"phname"`
	Phregno       string    `json:"phregno"`
	Contactname   string    `json:"contactname"`
	Contactphone  string    `json:"contactphone"`
	Contactemail  string    `json:"contactemail"`
	Streetaddress string    `json:"streetaddress"`
	DistrictID    int32     `json:"district_id"`
	Long          float64   `json:"long"`
	Lat           float64   `json:"lat"`
	Expirydate    time.Time `json:"expirydate"`
	Regstatus     string    `json:"regstatus"`
	ParishID      int32     `json:"parish_id"`
	VillageID     int32     `json:"village_id"`
}

func (q *Queries) CreatePackHouse(ctx context.Context, arg CreatePackHouseParams) (Packhouse, error) {
	row := q.db.QueryRowContext(ctx, createPackHouse,
		arg.Phname,
		arg.Phregno,
		arg.Contactname,
		arg.Contactphone,
		arg.Contactemail,
		arg.Streetaddress,
		arg.DistrictID,
		arg.Long,
		arg.Lat,
		arg.Expirydate,
		arg.Regstatus,
		arg.ParishID,
		arg.VillageID,
	)
	var i Packhouse
	err := row.Scan(
		&i.ID,
		&i.Phname,
		&i.Phregno,
		&i.Contactname,
		&i.Contactphone,
		&i.Contactemail,
		&i.Streetaddress,
		&i.DistrictID,
		&i.ParishID,
		&i.VillageID,
		&i.Long,
		&i.Lat,
		&i.Regstatus,
		&i.Expirydate,
		&i.CreatedAt,
	)
	return i, err
}

const deactivatePackhouse = `-- name: DeactivatePackhouse :one
UPDATE packhouses
  set
    regstatus = $2
WHERE id = $1
RETURNING id, phname, phregno, contactname, contactphone, contactemail, streetaddress, district_id, parish_id, village_id, long, lat, regstatus, expirydate, created_at
`

type DeactivatePackhouseParams struct {
	ID        int64  `json:"id"`
	Regstatus string `json:"regstatus"`
}

func (q *Queries) DeactivatePackhouse(ctx context.Context, arg DeactivatePackhouseParams) (Packhouse, error) {
	row := q.db.QueryRowContext(ctx, deactivatePackhouse, arg.ID, arg.Regstatus)
	var i Packhouse
	err := row.Scan(
		&i.ID,
		&i.Phname,
		&i.Phregno,
		&i.Contactname,
		&i.Contactphone,
		&i.Contactemail,
		&i.Streetaddress,
		&i.DistrictID,
		&i.ParishID,
		&i.VillageID,
		&i.Long,
		&i.Lat,
		&i.Regstatus,
		&i.Expirydate,
		&i.CreatedAt,
	)
	return i, err
}

const deletePackHouse = `-- name: DeletePackHouse :exec
DELETE FROM packhouses WHERE id= $1
`

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

const detachPackhouse = `-- name: DetachPackhouse :exec
DELETE FROM packhouse_exporter WHERE exportcompany_id= $1
`

func (q *Queries) DetachPackhouse(ctx context.Context, exportcompanyID int32) error {
	_, err := q.db.ExecContext(ctx, detachPackhouse, exportcompanyID)
	return err
}

const exporterPackhouses = `-- name: ExporterPackhouses :many
SELECT packhouses.phname,packhouses.phregno,packhouses.contactname,packhouses.contactphone,
packhouses.contactemail,packhouses.streetaddress FROM packhouse_exporter
LEFT JOIN packhouses on packhouses.id = packhouse_exporter.packhouse_id
WHERE packhouse_exporter.exportcompany_id = $1
`

type ExporterPackhousesRow struct {
	Phname        sql.NullString `json:"phname"`
	Phregno       sql.NullString `json:"phregno"`
	Contactname   sql.NullString `json:"contactname"`
	Contactphone  sql.NullString `json:"contactphone"`
	Contactemail  sql.NullString `json:"contactemail"`
	Streetaddress sql.NullString `json:"streetaddress"`
}

func (q *Queries) ExporterPackhouses(ctx context.Context, exportcompanyID int32) ([]ExporterPackhousesRow, error) {
	rows, err := q.db.QueryContext(ctx, exporterPackhouses, exportcompanyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ExporterPackhousesRow{}
	for rows.Next() {
		var i ExporterPackhousesRow
		if err := rows.Scan(
			&i.Phname,
			&i.Phregno,
			&i.Contactname,
			&i.Contactphone,
			&i.Contactemail,
			&i.Streetaddress,
		); 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 getCompanyPackhouses = `-- name: GetCompanyPackhouses :one
SELECT packhouse_id from packhouse_exporter
WHERE  exportcompany_id = $1
`

func (q *Queries) GetCompanyPackhouses(ctx context.Context, exportcompanyID int32) (int32, error) {
	row := q.db.QueryRowContext(ctx, getCompanyPackhouses, exportcompanyID)
	var packhouse_id int32
	err := row.Scan(&packhouse_id)
	return packhouse_id, err
}

const getExporterHouse = `-- name: GetExporterHouse :one
SELECT packhouse_id FROM packhouse_exporter
WHERE exportcompany_id = $1
`

func (q *Queries) GetExporterHouse(ctx context.Context, exportcompanyID int32) (int32, error) {
	row := q.db.QueryRowContext(ctx, getExporterHouse, exportcompanyID)
	var packhouse_id int32
	err := row.Scan(&packhouse_id)
	return packhouse_id, err
}

const getGlobalPackhouseCount = `-- name: GetGlobalPackhouseCount :one
SELECT COUNT(id) FROM packhouses
WHERE regstatus = $1
`

func (q *Queries) GetGlobalPackhouseCount(ctx context.Context, regstatus string) (int64, error) {
	row := q.db.QueryRowContext(ctx, getGlobalPackhouseCount, regstatus)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const getPackHouse = `-- name: GetPackHouse :one
SELECT packhouses.phname,packhouses.phregno,
packhouses.contactname,packhouses.contactphone,
packhouses.contactemail,packhouses.streetaddress,
packhouses.district_id,packhouses.long,packhouses.lat,
packhouses.expirydate,packhouses.regstatus,
packhouses.parish_id,exportcompanies.expsubcounty_id,
packhouses.village_id,packhouses.created_at, packhouses.id FROM packhouses
JOIN  packhouse_exporter  on packhouse_exporter.packhouse_id = packhouses.id
JOIN  exportcompanies     on exportcompanies.id  =  packhouse_exporter.exportcompany_id
WHERE packhouses.id = $1 LIMIT 1
`

type GetPackHouseRow struct {
	Phname         string    `json:"phname"`
	Phregno        string    `json:"phregno"`
	Contactname    string    `json:"contactname"`
	Contactphone   string    `json:"contactphone"`
	Contactemail   string    `json:"contactemail"`
	Streetaddress  string    `json:"streetaddress"`
	DistrictID     int32     `json:"district_id"`
	Long           float64   `json:"long"`
	Lat            float64   `json:"lat"`
	Expirydate     time.Time `json:"expirydate"`
	Regstatus      string    `json:"regstatus"`
	ParishID       int32     `json:"parish_id"`
	ExpsubcountyID int32     `json:"expsubcounty_id"`
	VillageID      int32     `json:"village_id"`
	CreatedAt      time.Time `json:"created_at"`
	ID             int64     `json:"id"`
}

func (q *Queries) GetPackHouse(ctx context.Context, id int64) (GetPackHouseRow, error) {
	row := q.db.QueryRowContext(ctx, getPackHouse, id)
	var i GetPackHouseRow
	err := row.Scan(
		&i.Phname,
		&i.Phregno,
		&i.Contactname,
		&i.Contactphone,
		&i.Contactemail,
		&i.Streetaddress,
		&i.DistrictID,
		&i.Long,
		&i.Lat,
		&i.Expirydate,
		&i.Regstatus,
		&i.ParishID,
		&i.ExpsubcountyID,
		&i.VillageID,
		&i.CreatedAt,
		&i.ID,
	)
	return i, err
}

const listPackHouses = `-- name: ListPackHouses :many
SELECT id, phname, phregno, contactname, contactphone, contactemail, streetaddress, district_id, parish_id, village_id, long, lat, regstatus, expirydate, created_at FROM packhouses
ORDER BY id LIMIT $1
OFFSET $2
`

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

func (q *Queries) ListPackHouses(ctx context.Context, arg ListPackHousesParams) ([]Packhouse, error) {
	rows, err := q.db.QueryContext(ctx, listPackHouses, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Packhouse{}
	for rows.Next() {
		var i Packhouse
		if err := rows.Scan(
			&i.ID,
			&i.Phname,
			&i.Phregno,
			&i.Contactname,
			&i.Contactphone,
			&i.Contactemail,
			&i.Streetaddress,
			&i.DistrictID,
			&i.ParishID,
			&i.VillageID,
			&i.Long,
			&i.Lat,
			&i.Regstatus,
			&i.Expirydate,
			&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 searchPackhouse = `-- name: SearchPackhouse :many
SELECT id, phname, phregno, contactname, contactphone, contactemail, streetaddress, district_id, parish_id, village_id, long, lat, regstatus, expirydate, created_at
FROM  packhouses
WHERE LOWER(phname) LIKE '%' || LOWER($1) || '%'
`

func (q *Queries) SearchPackhouse(ctx context.Context, lower string) ([]Packhouse, error) {
	rows, err := q.db.QueryContext(ctx, searchPackhouse, lower)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Packhouse{}
	for rows.Next() {
		var i Packhouse
		if err := rows.Scan(
			&i.ID,
			&i.Phname,
			&i.Phregno,
			&i.Contactname,
			&i.Contactphone,
			&i.Contactemail,
			&i.Streetaddress,
			&i.DistrictID,
			&i.ParishID,
			&i.VillageID,
			&i.Long,
			&i.Lat,
			&i.Regstatus,
			&i.Expirydate,
			&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 updatePackHouse = `-- name: UpdatePackHouse :one
UPDATE packhouses
  set
    phname= $2,
    phregno= $3,
    contactname= $4,
    contactphone= $5,
    contactemail= $6,
    streetaddress= $7,
    district_id= $8,
    parish_id= $9,
    village_id= $10,
    long = $11,
    lat = $12,
    expirydate = $13,
    regstatus  = $14
WHERE id = $1
RETURNING id, phname, phregno, contactname, contactphone, contactemail, streetaddress, district_id, parish_id, village_id, long, lat, regstatus, expirydate, created_at
`

type UpdatePackHouseParams struct {
	ID            int64     `json:"id"`
	Phname        string    `json:"phname"`
	Phregno       string    `json:"phregno"`
	Contactname   string    `json:"contactname"`
	Contactphone  string    `json:"contactphone"`
	Contactemail  string    `json:"contactemail"`
	Streetaddress string    `json:"streetaddress"`
	DistrictID    int32     `json:"district_id"`
	ParishID      int32     `json:"parish_id"`
	VillageID     int32     `json:"village_id"`
	Long          float64   `json:"long"`
	Lat           float64   `json:"lat"`
	Expirydate    time.Time `json:"expirydate"`
	Regstatus     string    `json:"regstatus"`
}

func (q *Queries) UpdatePackHouse(ctx context.Context, arg UpdatePackHouseParams) (Packhouse, error) {
	row := q.db.QueryRowContext(ctx, updatePackHouse,
		arg.ID,
		arg.Phname,
		arg.Phregno,
		arg.Contactname,
		arg.Contactphone,
		arg.Contactemail,
		arg.Streetaddress,
		arg.DistrictID,
		arg.ParishID,
		arg.VillageID,
		arg.Long,
		arg.Lat,
		arg.Expirydate,
		arg.Regstatus,
	)
	var i Packhouse
	err := row.Scan(
		&i.ID,
		&i.Phname,
		&i.Phregno,
		&i.Contactname,
		&i.Contactphone,
		&i.Contactemail,
		&i.Streetaddress,
		&i.DistrictID,
		&i.ParishID,
		&i.VillageID,
		&i.Long,
		&i.Lat,
		&i.Regstatus,
		&i.Expirydate,
		&i.CreatedAt,
	)
	return i, err
}

const updatePackhouseGps = `-- name: UpdatePackhouseGps :one
UPDATE packhouses
  set
    long = $2,
    lat  = $3
WHERE id = $1
RETURNING id, phname, phregno, contactname, contactphone, contactemail, streetaddress, district_id, parish_id, village_id, long, lat, regstatus, expirydate, created_at
`

type UpdatePackhouseGpsParams struct {
	ID   int64   `json:"id"`
	Long float64 `json:"long"`
	Lat  float64 `json:"lat"`
}

func (q *Queries) UpdatePackhouseGps(ctx context.Context, arg UpdatePackhouseGpsParams) (Packhouse, error) {
	row := q.db.QueryRowContext(ctx, updatePackhouseGps, arg.ID, arg.Long, arg.Lat)
	var i Packhouse
	err := row.Scan(
		&i.ID,
		&i.Phname,
		&i.Phregno,
		&i.Contactname,
		&i.Contactphone,
		&i.Contactemail,
		&i.Streetaddress,
		&i.DistrictID,
		&i.ParishID,
		&i.VillageID,
		&i.Long,
		&i.Lat,
		&i.Regstatus,
		&i.Expirydate,
		&i.CreatedAt,
	)
	return i, err
}

const updatePackhouseStatus = `-- name: UpdatePackhouseStatus :one
UPDATE packhouses
  set
    regstatus = $2
WHERE  id = $1
RETURNING id, phname, phregno, contactname, contactphone, contactemail, streetaddress, district_id, parish_id, village_id, long, lat, regstatus, expirydate, created_at
`

type UpdatePackhouseStatusParams struct {
	ID        int64  `json:"id"`
	Regstatus string `json:"regstatus"`
}

func (q *Queries) UpdatePackhouseStatus(ctx context.Context, arg UpdatePackhouseStatusParams) (Packhouse, error) {
	row := q.db.QueryRowContext(ctx, updatePackhouseStatus, arg.ID, arg.Regstatus)
	var i Packhouse
	err := row.Scan(
		&i.ID,
		&i.Phname,
		&i.Phregno,
		&i.Contactname,
		&i.Contactphone,
		&i.Contactemail,
		&i.Streetaddress,
		&i.DistrictID,
		&i.ParishID,
		&i.VillageID,
		&i.Long,
		&i.Lat,
		&i.Regstatus,
		&i.Expirydate,
		&i.CreatedAt,
	)
	return i, err
}

const userPackhouses = `-- name: UserPackhouses :many
SELECT packhouses.phname,packhouses.phregno,packhouses.contactname,packhouses.contactphone,
packhouses.contactemail,packhouses.streetaddress FROM user_packhouse
LEFT JOIN packhouses on packhouses.id = user_packhouse.packhouse_id
WHERE  user_packhouse.packhouse_id = $1
AND    user_packhouse.user_id = $2
`

type UserPackhousesParams struct {
	PackhouseID int32 `json:"packhouse_id"`
	UserID      int32 `json:"user_id"`
}

type UserPackhousesRow struct {
	Phname        sql.NullString `json:"phname"`
	Phregno       sql.NullString `json:"phregno"`
	Contactname   sql.NullString `json:"contactname"`
	Contactphone  sql.NullString `json:"contactphone"`
	Contactemail  sql.NullString `json:"contactemail"`
	Streetaddress sql.NullString `json:"streetaddress"`
}

func (q *Queries) UserPackhouses(ctx context.Context, arg UserPackhousesParams) ([]UserPackhousesRow, error) {
	rows, err := q.db.QueryContext(ctx, userPackhouses, arg.PackhouseID, arg.UserID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []UserPackhousesRow{}
	for rows.Next() {
		var i UserPackhousesRow
		if err := rows.Scan(
			&i.Phname,
			&i.Phregno,
			&i.Contactname,
			&i.Contactphone,
			&i.Contactemail,
			&i.Streetaddress,
		); 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
}
