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

package db

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

const allFarmers = `-- name: AllFarmers :many
SELECT id, fmcode, fmid, fmidtype, fmname, fmphoto, fmphone, fmemail, status, isverified, created_at FROM farmers
`

func (q *Queries) AllFarmers(ctx context.Context) ([]Farmer, error) {
	rows, err := q.db.QueryContext(ctx, allFarmers)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Farmer{}
	for rows.Next() {
		var i Farmer
		if err := rows.Scan(
			&i.ID,
			&i.Fmcode,
			&i.Fmid,
			&i.Fmidtype,
			&i.Fmname,
			&i.Fmphoto,
			&i.Fmphone,
			&i.Fmemail,
			&i.Status,
			&i.Isverified,
			&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 allFarmersForExporters = `-- name: AllFarmersForExporters :many
SELECT farmers.fmcode,farmers.id
farmer_id,farmers.fmid,farmers.fmidtype,farmers.fmname,farmers.fmphone,farmers.created_at farmer_created_at, farmers.status,
farmers.fmemail FROM farmers
LEFT  JOIN farmer_exporter on farmer_exporter.farmer_id = farmers.id
LEFT  JOIN farmer_farms  on farmer_farms.farmer_id  = farmers.id
WHERE farmer_exporter.exportcompany_id = $1
AND   farmer_farms.crop_id = $2
ORDER BY farmers.id
`

type AllFarmersForExportersParams struct {
	ExportcompanyID int32 `json:"exportcompany_id"`
	CropID          int32 `json:"crop_id"`
}

type AllFarmersForExportersRow struct {
	Fmcode          sql.NullString `json:"fmcode"`
	FarmerID        int64          `json:"farmer_id"`
	Fmid            string         `json:"fmid"`
	Fmidtype        string         `json:"fmidtype"`
	Fmname          string         `json:"fmname"`
	Fmphone         string         `json:"fmphone"`
	FarmerCreatedAt time.Time      `json:"farmer_created_at"`
	Status          string         `json:"status"`
	Fmemail         sql.NullString `json:"fmemail"`
}

func (q *Queries) AllFarmersForExporters(ctx context.Context, arg AllFarmersForExportersParams) ([]AllFarmersForExportersRow, error) {
	rows, err := q.db.QueryContext(ctx, allFarmersForExporters, arg.ExportcompanyID, arg.CropID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []AllFarmersForExportersRow{}
	for rows.Next() {
		var i AllFarmersForExportersRow
		if err := rows.Scan(
			&i.Fmcode,
			&i.FarmerID,
			&i.Fmid,
			&i.Fmidtype,
			&i.Fmname,
			&i.Fmphone,
			&i.FarmerCreatedAt,
			&i.Status,
			&i.Fmemail,
		); 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 assignFarmerToExporter = `-- name: AssignFarmerToExporter :one
INSERT INTO farmer_exporter (
    "farmer_id",
    "exportcompany_id"
 ) VALUES (
     $1,
     $2
  )
RETURNING id, farmer_id, exportcompany_id, created_at
`

type AssignFarmerToExporterParams struct {
	FarmerID        int32 `json:"farmer_id"`
	ExportcompanyID int32 `json:"exportcompany_id"`
}

func (q *Queries) AssignFarmerToExporter(ctx context.Context, arg AssignFarmerToExporterParams) (FarmerExporter, error) {
	row := q.db.QueryRowContext(ctx, assignFarmerToExporter, arg.FarmerID, arg.ExportcompanyID)
	var i FarmerExporter
	err := row.Scan(
		&i.ID,
		&i.FarmerID,
		&i.ExportcompanyID,
		&i.CreatedAt,
	)
	return i, err
}

const changeFarmerStatus = `-- name: ChangeFarmerStatus :one
UPDATE farmers
  set
    status = $2
WHERE id = $1
RETURNING id, fmcode, fmid, fmidtype, fmname, fmphoto, fmphone, fmemail, status, isverified, created_at
`

type ChangeFarmerStatusParams struct {
	ID     int64  `json:"id"`
	Status string `json:"status"`
}

func (q *Queries) ChangeFarmerStatus(ctx context.Context, arg ChangeFarmerStatusParams) (Farmer, error) {
	row := q.db.QueryRowContext(ctx, changeFarmerStatus, arg.ID, arg.Status)
	var i Farmer
	err := row.Scan(
		&i.ID,
		&i.Fmcode,
		&i.Fmid,
		&i.Fmidtype,
		&i.Fmname,
		&i.Fmphoto,
		&i.Fmphone,
		&i.Fmemail,
		&i.Status,
		&i.Isverified,
		&i.CreatedAt,
	)
	return i, err
}

const checkFarmerExistanceExporter = `-- name: CheckFarmerExistanceExporter :one
SELECT EXISTS(
  SELECT 1 FROM farmer_exporter
  WHERE farmer_id = $1
  AND exportcompany_id = $2
)
`

type CheckFarmerExistanceExporterParams struct {
	FarmerID        int32 `json:"farmer_id"`
	ExportcompanyID int32 `json:"exportcompany_id"`
}

func (q *Queries) CheckFarmerExistanceExporter(ctx context.Context, arg CheckFarmerExistanceExporterParams) (bool, error) {
	row := q.db.QueryRowContext(ctx, checkFarmerExistanceExporter, arg.FarmerID, arg.ExportcompanyID)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const countFarmerFarms = `-- name: CountFarmerFarms :one
SELECT COUNT(*) from farmer_farms
`

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

const countTraceabilityCodes = `-- name: CountTraceabilityCodes :one
SELECT COUNT(farmer_farms.traceability_code) AS traceability_code_count
FROM farmer_farms
JOIN  redflags ON redflags.traceability_code = farmer_farms.traceability_code
WHERE redflags.status IN ('RFapproved', 'RFpending')
`

func (q *Queries) CountTraceabilityCodes(ctx context.Context) (int64, error) {
	row := q.db.QueryRowContext(ctx, countTraceabilityCodes)
	var traceability_code_count int64
	err := row.Scan(&traceability_code_count)
	return traceability_code_count, err
}

const createFarmer = `-- name: CreateFarmer :one
INSERT INTO farmers (
    "fmcode",
    "fmid",
    "fmidtype",
    "fmname",
    "fmphone",
    "fmemail"
 ) VALUES (
     $1,
     $2,
     $3,
     $4,
     $5,
     $6
  )
RETURNING id, fmcode, fmid, fmidtype, fmname, fmphoto, fmphone, fmemail, status, isverified, created_at
`

type CreateFarmerParams struct {
	Fmcode   sql.NullString `json:"fmcode"`
	Fmid     string         `json:"fmid"`
	Fmidtype string         `json:"fmidtype"`
	Fmname   string         `json:"fmname"`
	Fmphone  string         `json:"fmphone"`
	Fmemail  sql.NullString `json:"fmemail"`
}

func (q *Queries) CreateFarmer(ctx context.Context, arg CreateFarmerParams) (Farmer, error) {
	row := q.db.QueryRowContext(ctx, createFarmer,
		arg.Fmcode,
		arg.Fmid,
		arg.Fmidtype,
		arg.Fmname,
		arg.Fmphone,
		arg.Fmemail,
	)
	var i Farmer
	err := row.Scan(
		&i.ID,
		&i.Fmcode,
		&i.Fmid,
		&i.Fmidtype,
		&i.Fmname,
		&i.Fmphoto,
		&i.Fmphone,
		&i.Fmemail,
		&i.Status,
		&i.Isverified,
		&i.CreatedAt,
	)
	return i, err
}

const deleteFarmer = `-- name: DeleteFarmer :exec
DELETE FROM farmers WHERE id= $1
`

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

const deleteFarmerContacts = `-- name: DeleteFarmerContacts :exec
DELETE FROM farmer_contacts WHERE id= $1
`

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

const deleteFarmerFarmByID = `-- name: DeleteFarmerFarmByID :exec
DELETE FROM farmer_farms where id = $1
`

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

const deleteFarmerFarms = `-- name: DeleteFarmerFarms :exec
DELETE FROM farmer_farms WHERE id= $1
`

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

const deleteFarmerProduce = `-- name: DeleteFarmerProduce :exec
DELETE FROM farmer_produce WHERE id= $1
`

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

const detachFarmerFromExporter = `-- name: DetachFarmerFromExporter :exec
DELETE FROM farmer_exporter where
farmer_id = $1 AND
exportcompany_id= $2
`

type DetachFarmerFromExporterParams struct {
	FarmerID        int32 `json:"farmer_id"`
	ExportcompanyID int32 `json:"exportcompany_id"`
}

func (q *Queries) DetachFarmerFromExporter(ctx context.Context, arg DetachFarmerFromExporterParams) error {
	_, err := q.db.ExecContext(ctx, detachFarmerFromExporter, arg.FarmerID, arg.ExportcompanyID)
	return err
}

const exportFarmersWithFarms = `-- name: ExportFarmersWithFarms :many
SELECT farmers.id as farmer_id,farmers.fmcode,farmers.fmid,
farmers.fmidtype,farmers.fmname,farmers.fmphone,
farmers.fmemail,farmer_farms.traceability_code,farmer_farms.acreage,
farmer_farms.lat,farmer_farms.long,crops.cropname,crops.crophscode,
crops.riskstatus,crops.cic_code from farmer_farms
INNER JOIN farmers on farmer_farms.farmer_id = farmers.id
INNER JOIN crops  on crops.id = farmer_farms.crop_id
`

type ExportFarmersWithFarmsRow struct {
	FarmerID         int64          `json:"farmer_id"`
	Fmcode           sql.NullString `json:"fmcode"`
	Fmid             string         `json:"fmid"`
	Fmidtype         string         `json:"fmidtype"`
	Fmname           string         `json:"fmname"`
	Fmphone          string         `json:"fmphone"`
	Fmemail          sql.NullString `json:"fmemail"`
	TraceabilityCode string         `json:"traceability_code"`
	Acreage          float64        `json:"acreage"`
	Lat              float64        `json:"lat"`
	Long             float64        `json:"long"`
	Cropname         string         `json:"cropname"`
	Crophscode       string         `json:"crophscode"`
	Riskstatus       string         `json:"riskstatus"`
	CicCode          string         `json:"cic_code"`
}

func (q *Queries) ExportFarmersWithFarms(ctx context.Context) ([]ExportFarmersWithFarmsRow, error) {
	rows, err := q.db.QueryContext(ctx, exportFarmersWithFarms)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ExportFarmersWithFarmsRow{}
	for rows.Next() {
		var i ExportFarmersWithFarmsRow
		if err := rows.Scan(
			&i.FarmerID,
			&i.Fmcode,
			&i.Fmid,
			&i.Fmidtype,
			&i.Fmname,
			&i.Fmphone,
			&i.Fmemail,
			&i.TraceabilityCode,
			&i.Acreage,
			&i.Lat,
			&i.Long,
			&i.Cropname,
			&i.Crophscode,
			&i.Riskstatus,
			&i.CicCode,
		); 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 exporterFarmers = `-- name: ExporterFarmers :many
SELECT farmers.fmcode,farmers.id farmer_id,farmers.fmid,farmers.fmidtype,farmers.fmname,farmers.fmphone,farmers.created_at farmer_created_at, farmers.status,
farmers.fmemail,farmer_farms.traceability_code,farmer_farms.long, farmer_farms.lat,farmer_farms.acreage FROM farmers
INNER JOIN farmer_farms on farmer_farms.farmer_id = farmers.id
INNER JOIN farmer_exporter on farmer_exporter.farmer_id = farmers.id
WHERE farmer_exporter.exportcompany_id = $1
ORDER BY farmers.id
`

type ExporterFarmersRow struct {
	Fmcode           sql.NullString `json:"fmcode"`
	FarmerID         int64          `json:"farmer_id"`
	Fmid             string         `json:"fmid"`
	Fmidtype         string         `json:"fmidtype"`
	Fmname           string         `json:"fmname"`
	Fmphone          string         `json:"fmphone"`
	FarmerCreatedAt  time.Time      `json:"farmer_created_at"`
	Status           string         `json:"status"`
	Fmemail          sql.NullString `json:"fmemail"`
	TraceabilityCode string         `json:"traceability_code"`
	Long             float64        `json:"long"`
	Lat              float64        `json:"lat"`
	Acreage          float64        `json:"acreage"`
}

func (q *Queries) ExporterFarmers(ctx context.Context, exportcompanyID int32) ([]ExporterFarmersRow, error) {
	rows, err := q.db.QueryContext(ctx, exporterFarmers, exportcompanyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ExporterFarmersRow{}
	for rows.Next() {
		var i ExporterFarmersRow
		if err := rows.Scan(
			&i.Fmcode,
			&i.FarmerID,
			&i.Fmid,
			&i.Fmidtype,
			&i.Fmname,
			&i.Fmphone,
			&i.FarmerCreatedAt,
			&i.Status,
			&i.Fmemail,
			&i.TraceabilityCode,
			&i.Long,
			&i.Lat,
			&i.Acreage,
		); 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 farmDeactivation = `-- name: FarmDeactivation :one
UPDATE farmer_farms
  set
    status = 'InActive'
WHERE id = $1
RETURNING id, farmer_id, crop_id, traceability_code, harvestbalance, district_id, subcounty_id, parish_id, village_id, acreage, lat, long, created_at, plantingdate, status
`

func (q *Queries) FarmDeactivation(ctx context.Context, id int64) (FarmerFarm, error) {
	row := q.db.QueryRowContext(ctx, farmDeactivation, id)
	var i FarmerFarm
	err := row.Scan(
		&i.ID,
		&i.FarmerID,
		&i.CropID,
		&i.TraceabilityCode,
		&i.Harvestbalance,
		&i.DistrictID,
		&i.SubcountyID,
		&i.ParishID,
		&i.VillageID,
		&i.Acreage,
		&i.Lat,
		&i.Long,
		&i.CreatedAt,
		&i.Plantingdate,
		&i.Status,
	)
	return i, err
}

const farmerContacts = `-- name: FarmerContacts :one
INSERT INTO farmer_contacts (
    "farmer_id",
    "contactname",
    "contactphone",
    "contactemail"
 ) VALUES (
     $1,
     $2,
     $3,
     $4
  )
RETURNING id, farmer_id, contactname, contactphone, contactemail, created_at
`

type FarmerContactsParams struct {
	FarmerID     int32  `json:"farmer_id"`
	Contactname  string `json:"contactname"`
	Contactphone string `json:"contactphone"`
	Contactemail string `json:"contactemail"`
}

func (q *Queries) FarmerContacts(ctx context.Context, arg FarmerContactsParams) (FarmerContact, error) {
	row := q.db.QueryRowContext(ctx, farmerContacts,
		arg.FarmerID,
		arg.Contactname,
		arg.Contactphone,
		arg.Contactemail,
	)
	var i FarmerContact
	err := row.Scan(
		&i.ID,
		&i.FarmerID,
		&i.Contactname,
		&i.Contactphone,
		&i.Contactemail,
		&i.CreatedAt,
	)
	return i, err
}

const farmerCount = `-- name: FarmerCount :one
SELECT COUNT(id) from farmers
`

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

const farmerCountBasedOne = `-- name: FarmerCountBasedOne :one
SELECT COUNT(farmers.id) from farmers
WHERE farmers.status = $1
`

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

const farmerCropsFarmsCount = `-- name: FarmerCropsFarmsCount :one
SELECT farmer_farms.crop_id, COUNT(farmer_farms.id)
FROM farmer_farms
WHERE farmer_farms.farmer_id = $1
GROUP BY farmer_farms.crop_id
`

type FarmerCropsFarmsCountRow struct {
	CropID int32 `json:"crop_id"`
	Count  int64 `json:"count"`
}

func (q *Queries) FarmerCropsFarmsCount(ctx context.Context, farmerID int32) (FarmerCropsFarmsCountRow, error) {
	row := q.db.QueryRowContext(ctx, farmerCropsFarmsCount, farmerID)
	var i FarmerCropsFarmsCountRow
	err := row.Scan(&i.CropID, &i.Count)
	return i, err
}

const farmerExporterCount = `-- name: FarmerExporterCount :one
SELECT COUNT(farmers.id) from farmers
LEFT JOIN  farmer_exporter  on farmer_exporter.farmer_id = farmers.id
LEFT JOIN  exportcompanies  on exportcompanies.id     = farmer_exporter.exportcompany_id
WHERE exportcompanies.id = $2
AND   farmers.status = $1
`

type FarmerExporterCountParams struct {
	Status string `json:"status"`
	ID     int64  `json:"id"`
}

func (q *Queries) FarmerExporterCount(ctx context.Context, arg FarmerExporterCountParams) (int64, error) {
	row := q.db.QueryRowContext(ctx, farmerExporterCount, arg.Status, arg.ID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const farmerFarms = `-- name: FarmerFarms :one
INSERT INTO farmer_farms (
    "farmer_id",
    "traceability_code",
    "district_id",
    "harvestbalance",
    "crop_id",
    "parish_id",
    "village_id",
    "subcounty_id",
    "acreage",
    "lat",
    "long"
 ) VALUES (
     $1,
     $2,
     $3,
     $4,
     $5,
     $6,
     $7,
     $8,
     $9,
     $10,
     $11
  )
RETURNING id, farmer_id, crop_id, traceability_code, harvestbalance, district_id, subcounty_id, parish_id, village_id, acreage, lat, long, created_at, plantingdate, status
`

type FarmerFarmsParams struct {
	FarmerID         int32           `json:"farmer_id"`
	TraceabilityCode string          `json:"traceability_code"`
	DistrictID       int32           `json:"district_id"`
	Harvestbalance   sql.NullFloat64 `json:"harvestbalance"`
	CropID           int32           `json:"crop_id"`
	ParishID         int32           `json:"parish_id"`
	VillageID        int32           `json:"village_id"`
	SubcountyID      int32           `json:"subcounty_id"`
	Acreage          float64         `json:"acreage"`
	Lat              float64         `json:"lat"`
	Long             float64         `json:"long"`
}

func (q *Queries) FarmerFarms(ctx context.Context, arg FarmerFarmsParams) (FarmerFarm, error) {
	row := q.db.QueryRowContext(ctx, farmerFarms,
		arg.FarmerID,
		arg.TraceabilityCode,
		arg.DistrictID,
		arg.Harvestbalance,
		arg.CropID,
		arg.ParishID,
		arg.VillageID,
		arg.SubcountyID,
		arg.Acreage,
		arg.Lat,
		arg.Long,
	)
	var i FarmerFarm
	err := row.Scan(
		&i.ID,
		&i.FarmerID,
		&i.CropID,
		&i.TraceabilityCode,
		&i.Harvestbalance,
		&i.DistrictID,
		&i.SubcountyID,
		&i.ParishID,
		&i.VillageID,
		&i.Acreage,
		&i.Lat,
		&i.Long,
		&i.CreatedAt,
		&i.Plantingdate,
		&i.Status,
	)
	return i, err
}

const farmerFarmsCount = `-- name: FarmerFarmsCount :one
SELECT farmer_farms.village_id, COUNT(farmer_farms.id)
FROM farmer_farms
WHERE farmer_farms.farmer_id = $1
GROUP BY farmer_farms.village_id
`

type FarmerFarmsCountRow struct {
	VillageID int32 `json:"village_id"`
	Count     int64 `json:"count"`
}

func (q *Queries) FarmerFarmsCount(ctx context.Context, farmerID int32) (FarmerFarmsCountRow, error) {
	row := q.db.QueryRowContext(ctx, farmerFarmsCount, farmerID)
	var i FarmerFarmsCountRow
	err := row.Scan(&i.VillageID, &i.Count)
	return i, err
}

const farmerProduce = `-- name: FarmerProduce :one
INSERT INTO farmer_produce (
    "farmer_id",
    "crop_id",
    "plantingdate",
    "acreage"
 ) VALUES (
     $1,
     $2,
     $3,
     $4
  )
RETURNING id, farmer_id, crop_id, plantingdate, acreage, created_at
`

type FarmerProduceParams struct {
	FarmerID     int32     `json:"farmer_id"`
	CropID       int32     `json:"crop_id"`
	Plantingdate time.Time `json:"plantingdate"`
	Acreage      float64   `json:"acreage"`
}

func (q *Queries) FarmerProduce(ctx context.Context, arg FarmerProduceParams) (FarmerProduce, error) {
	row := q.db.QueryRowContext(ctx, farmerProduce,
		arg.FarmerID,
		arg.CropID,
		arg.Plantingdate,
		arg.Acreage,
	)
	var i FarmerProduce
	err := row.Scan(
		&i.ID,
		&i.FarmerID,
		&i.CropID,
		&i.Plantingdate,
		&i.Acreage,
		&i.CreatedAt,
	)
	return i, err
}

const farmersForExporters = `-- name: FarmersForExporters :many
SELECT farmers.fmcode,farmers.id farmer_id,farmers.fmid,farmers.status farmer_status,farmers.fmidtype,farmers.fmname,farmers.fmphone,farmers.created_at farmer_created_at, farmers.status,
farmers.fmemail FROM farmers
INNER JOIN farmer_exporter on farmer_exporter.farmer_id = farmers.id
WHERE farmer_exporter.exportcompany_id = $3
ORDER BY farmers.id
LIMIT $1 OFFSET $2
`

type FarmersForExportersParams struct {
	Limit           int32 `json:"limit"`
	Offset          int32 `json:"offset"`
	ExportcompanyID int32 `json:"exportcompany_id"`
}

type FarmersForExportersRow struct {
	Fmcode          sql.NullString `json:"fmcode"`
	FarmerID        int64          `json:"farmer_id"`
	Fmid            string         `json:"fmid"`
	FarmerStatus    string         `json:"farmer_status"`
	Fmidtype        string         `json:"fmidtype"`
	Fmname          string         `json:"fmname"`
	Fmphone         string         `json:"fmphone"`
	FarmerCreatedAt time.Time      `json:"farmer_created_at"`
	Status          string         `json:"status"`
	Fmemail         sql.NullString `json:"fmemail"`
}

func (q *Queries) FarmersForExporters(ctx context.Context, arg FarmersForExportersParams) ([]FarmersForExportersRow, error) {
	rows, err := q.db.QueryContext(ctx, farmersForExporters, arg.Limit, arg.Offset, arg.ExportcompanyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []FarmersForExportersRow{}
	for rows.Next() {
		var i FarmersForExportersRow
		if err := rows.Scan(
			&i.Fmcode,
			&i.FarmerID,
			&i.Fmid,
			&i.FarmerStatus,
			&i.Fmidtype,
			&i.Fmname,
			&i.Fmphone,
			&i.FarmerCreatedAt,
			&i.Status,
			&i.Fmemail,
		); 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 fetchTraceabilityCodes = `-- name: FetchTraceabilityCodes :many
SELECT farmer_farms.traceability_code,redflags.status,
farmer_farms.acreage,farmer_farms.harvestbalance,
crops.cropname,farmers.fmname
from farmer_farms
LEFT JOIN crops    on crops.id          = farmer_farms.crop_id
LEFT JOIN redflags on redflags.traceability_code = farmer_farms.traceability_code
LEFT JOIN farmers  on farmers.id        = farmer_farms.farmer_id
LIMIT $1 OFFSET $2
`

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

type FetchTraceabilityCodesRow struct {
	TraceabilityCode string          `json:"traceability_code"`
	Status           sql.NullString  `json:"status"`
	Acreage          float64         `json:"acreage"`
	Harvestbalance   sql.NullFloat64 `json:"harvestbalance"`
	Cropname         sql.NullString  `json:"cropname"`
	Fmname           sql.NullString  `json:"fmname"`
}

func (q *Queries) FetchTraceabilityCodes(ctx context.Context, arg FetchTraceabilityCodesParams) ([]FetchTraceabilityCodesRow, error) {
	rows, err := q.db.QueryContext(ctx, fetchTraceabilityCodes, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []FetchTraceabilityCodesRow{}
	for rows.Next() {
		var i FetchTraceabilityCodesRow
		if err := rows.Scan(
			&i.TraceabilityCode,
			&i.Status,
			&i.Acreage,
			&i.Harvestbalance,
			&i.Cropname,
			&i.Fmname,
		); 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 getContactsForFarmer = `-- name: GetContactsForFarmer :many
SELECT contactname,contactphone,contactemail
FROM farmer_contacts
WHERE farmer_id = $1
`

type GetContactsForFarmerRow struct {
	Contactname  string `json:"contactname"`
	Contactphone string `json:"contactphone"`
	Contactemail string `json:"contactemail"`
}

func (q *Queries) GetContactsForFarmer(ctx context.Context, farmerID int32) ([]GetContactsForFarmerRow, error) {
	rows, err := q.db.QueryContext(ctx, getContactsForFarmer, farmerID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetContactsForFarmerRow{}
	for rows.Next() {
		var i GetContactsForFarmerRow
		if err := rows.Scan(&i.Contactname, &i.Contactphone, &i.Contactemail); 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 getFarmer = `-- name: GetFarmer :one
SELECT id, fmcode, fmid, fmidtype, fmname, fmphoto, fmphone, fmemail, status, isverified, created_at FROM farmers
WHERE id = $1 LIMIT 1
`

func (q *Queries) GetFarmer(ctx context.Context, id int64) (Farmer, error) {
	row := q.db.QueryRowContext(ctx, getFarmer, id)
	var i Farmer
	err := row.Scan(
		&i.ID,
		&i.Fmcode,
		&i.Fmid,
		&i.Fmidtype,
		&i.Fmname,
		&i.Fmphoto,
		&i.Fmphone,
		&i.Fmemail,
		&i.Status,
		&i.Isverified,
		&i.CreatedAt,
	)
	return i, err
}

const getFarmerBasedCrop = `-- name: GetFarmerBasedCrop :many
SELECT farmer_farms.farmer_id,farmer_farms.traceability_code,
farmers.fmcode,farmers.fmname, crops.cropname, crops.id ,crops.variant FROM farmer_farms
JOIN  farmers   on  farmers.id   = farmer_farms.farmer_id
JOIN  crops     on  crops.id     = farmer_farms.crop_id
WHERE farmer_farms.crop_id = $1
`

type GetFarmerBasedCropRow struct {
	FarmerID         int32          `json:"farmer_id"`
	TraceabilityCode string         `json:"traceability_code"`
	Fmcode           sql.NullString `json:"fmcode"`
	Fmname           string         `json:"fmname"`
	Cropname         string         `json:"cropname"`
	ID               int64          `json:"id"`
	Variant          string         `json:"variant"`
}

func (q *Queries) GetFarmerBasedCrop(ctx context.Context, cropID int32) ([]GetFarmerBasedCropRow, error) {
	rows, err := q.db.QueryContext(ctx, getFarmerBasedCrop, cropID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetFarmerBasedCropRow{}
	for rows.Next() {
		var i GetFarmerBasedCropRow
		if err := rows.Scan(
			&i.FarmerID,
			&i.TraceabilityCode,
			&i.Fmcode,
			&i.Fmname,
			&i.Cropname,
			&i.ID,
			&i.Variant,
		); 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 getFarmerContacts = `-- name: GetFarmerContacts :many
SELECT id, farmer_id, contactname, contactphone, contactemail, created_at FROM farmer_contacts
WHERE farmer_id = $1 LIMIT 1
`

func (q *Queries) GetFarmerContacts(ctx context.Context, farmerID int32) ([]FarmerContact, error) {
	rows, err := q.db.QueryContext(ctx, getFarmerContacts, farmerID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []FarmerContact{}
	for rows.Next() {
		var i FarmerContact
		if err := rows.Scan(
			&i.ID,
			&i.FarmerID,
			&i.Contactname,
			&i.Contactphone,
			&i.Contactemail,
			&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 getFarmerDetailsBasedOnReception = `-- name: GetFarmerDetailsBasedOnReception :one
SELECT farmers.fmphone,farmers.fmname,farmer_farms.traceability_code,receptionform.netweight,packhouses.phname, crops.cropname FROM farmers
JOIN   receptionform on receptionform.supplier_id = farmers.id
JOIN   farmer_farms  on farmer_farms.farmer_id    = farmers.id
JOIN   packhouses    on packhouses.id             = receptionform.packhouse_id
JOIN   crops         on crops.id                  = receptionform.crop_id
WHERE  receptionform.id = $1
`

type GetFarmerDetailsBasedOnReceptionRow struct {
	Fmphone          string  `json:"fmphone"`
	Fmname           string  `json:"fmname"`
	TraceabilityCode string  `json:"traceability_code"`
	Netweight        float64 `json:"netweight"`
	Phname           string  `json:"phname"`
	Cropname         string  `json:"cropname"`
}

func (q *Queries) GetFarmerDetailsBasedOnReception(ctx context.Context, id int64) (GetFarmerDetailsBasedOnReceptionRow, error) {
	row := q.db.QueryRowContext(ctx, getFarmerDetailsBasedOnReception, id)
	var i GetFarmerDetailsBasedOnReceptionRow
	err := row.Scan(
		&i.Fmphone,
		&i.Fmname,
		&i.TraceabilityCode,
		&i.Netweight,
		&i.Phname,
		&i.Cropname,
	)
	return i, err
}

const getFarmerFarms = `-- name: GetFarmerFarms :many
SELECT farmer_farms.farmer_id,farmer_farms.traceability_code,crops.cropname,crops.variant,
crops.crophscode,crops.riskstatus,crops.cic_code,farmer_farms.plantingdate,
COALESCE(redflags.status) AS redflag_status,farmer_farms.id as farm_id FROM farmer_farms
INNER JOIN crops    on crops.id = farmer_farms.crop_id
LEFT  JOIN redflags on redflags.traceability_code = farmer_farms.traceability_code
WHERE farmer_farms.farmer_id = $1
`

type GetFarmerFarmsRow struct {
	FarmerID         int32          `json:"farmer_id"`
	TraceabilityCode string         `json:"traceability_code"`
	Cropname         string         `json:"cropname"`
	Variant          string         `json:"variant"`
	Crophscode       string         `json:"crophscode"`
	Riskstatus       string         `json:"riskstatus"`
	CicCode          string         `json:"cic_code"`
	Plantingdate     sql.NullTime   `json:"plantingdate"`
	RedflagStatus    sql.NullString `json:"redflag_status"`
	FarmID           int64          `json:"farm_id"`
}

func (q *Queries) GetFarmerFarms(ctx context.Context, farmerID int32) ([]GetFarmerFarmsRow, error) {
	rows, err := q.db.QueryContext(ctx, getFarmerFarms, farmerID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetFarmerFarmsRow{}
	for rows.Next() {
		var i GetFarmerFarmsRow
		if err := rows.Scan(
			&i.FarmerID,
			&i.TraceabilityCode,
			&i.Cropname,
			&i.Variant,
			&i.Crophscode,
			&i.Riskstatus,
			&i.CicCode,
			&i.Plantingdate,
			&i.RedflagStatus,
			&i.FarmID,
		); 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 getFarmerIDByCode = `-- name: GetFarmerIDByCode :one
SELECT id
FROM farmers
WHERE fmcode = $1
LIMIT 1
`

func (q *Queries) GetFarmerIDByCode(ctx context.Context, fmcode sql.NullString) (int64, error) {
	row := q.db.QueryRowContext(ctx, getFarmerIDByCode, fmcode)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const getFarmerIDByTraceability = `-- name: GetFarmerIDByTraceability :one
SELECT traceability_code,farmers.id farmers_id from farmer_farms
JOIN farmers on farmers.id = farmer_farms.farmer_id
WHERE traceability_code = $1
`

type GetFarmerIDByTraceabilityRow struct {
	TraceabilityCode string `json:"traceability_code"`
	FarmersID        int64  `json:"farmers_id"`
}

func (q *Queries) GetFarmerIDByTraceability(ctx context.Context, traceabilityCode string) (GetFarmerIDByTraceabilityRow, error) {
	row := q.db.QueryRowContext(ctx, getFarmerIDByTraceability, traceabilityCode)
	var i GetFarmerIDByTraceabilityRow
	err := row.Scan(&i.TraceabilityCode, &i.FarmersID)
	return i, err
}

const getFarmsForFarmer = `-- name: GetFarmsForFarmer :many
SELECT DISTINCT on (crops.id) crops.id as crop_id,farmer_farms.farmer_id,farmer_farms.traceability_code,districts.name as district_name,
parishes.parish as parish_name,villages.village as village_name,subcounties.name,COALESCE(redflags.status) AS redflag_status,
acreage,lat,long,crops.cropname,crops.variant,crops.crophscode,farmer_farms.id as farm_id,
crops.riskstatus,crops.cic_code,villages.id as village_id,subcounties.id as subcounty_id,
parishes.id as parishes_id,districts.id as district_id,farmer_farms.plantingdate,subcounties.name as subcountyname,
farmer_farms.harvestbalance FROM farmer_farms
JOIN  districts   on districts.id = farmer_farms.district_id
JOIN  subcounties on subcounties.district_id = districts.id
JOIN  parishes    on parishes.id  = farmer_farms.parish_id
JOIN  villages    on villages.id  = farmer_farms.village_id
JOIN  crops       on crops.id     = farmer_farms.crop_id
LEFT  JOIN  redflags    on redflags.traceability_code = farmer_farms.traceability_code
WHERE farmer_farms.farmer_id = $1
`

type GetFarmsForFarmerRow struct {
	CropID           int64           `json:"crop_id"`
	FarmerID         int32           `json:"farmer_id"`
	TraceabilityCode string          `json:"traceability_code"`
	DistrictName     string          `json:"district_name"`
	ParishName       string          `json:"parish_name"`
	VillageName      string          `json:"village_name"`
	Name             string          `json:"name"`
	RedflagStatus    sql.NullString  `json:"redflag_status"`
	Acreage          float64         `json:"acreage"`
	Lat              float64         `json:"lat"`
	Long             float64         `json:"long"`
	Cropname         string          `json:"cropname"`
	Variant          string          `json:"variant"`
	Crophscode       string          `json:"crophscode"`
	FarmID           int64           `json:"farm_id"`
	Riskstatus       string          `json:"riskstatus"`
	CicCode          string          `json:"cic_code"`
	VillageID        int64           `json:"village_id"`
	SubcountyID      int64           `json:"subcounty_id"`
	ParishesID       int64           `json:"parishes_id"`
	DistrictID       int64           `json:"district_id"`
	Plantingdate     sql.NullTime    `json:"plantingdate"`
	Subcountyname    string          `json:"subcountyname"`
	Harvestbalance   sql.NullFloat64 `json:"harvestbalance"`
}

func (q *Queries) GetFarmsForFarmer(ctx context.Context, farmerID int32) ([]GetFarmsForFarmerRow, error) {
	rows, err := q.db.QueryContext(ctx, getFarmsForFarmer, farmerID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetFarmsForFarmerRow{}
	for rows.Next() {
		var i GetFarmsForFarmerRow
		if err := rows.Scan(
			&i.CropID,
			&i.FarmerID,
			&i.TraceabilityCode,
			&i.DistrictName,
			&i.ParishName,
			&i.VillageName,
			&i.Name,
			&i.RedflagStatus,
			&i.Acreage,
			&i.Lat,
			&i.Long,
			&i.Cropname,
			&i.Variant,
			&i.Crophscode,
			&i.FarmID,
			&i.Riskstatus,
			&i.CicCode,
			&i.VillageID,
			&i.SubcountyID,
			&i.ParishesID,
			&i.DistrictID,
			&i.Plantingdate,
			&i.Subcountyname,
			&i.Harvestbalance,
		); 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 getLastID = `-- name: GetLastID :one
SELECT id FROM farmers WHERE id = (SELECT MAX(id) FROM farmers)
`

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

const getProduceForFarmer = `-- name: GetProduceForFarmer :many
SELECT crops.cropname as crop_name,crops.id crop_id,
crophscode,riskstatus,cic_code,
plantingdate,acreage
FROM  farmer_produce
JOIN  crops on crops.id = farmer_produce.crop_id
WHERE farmer_id = $1
`

type GetProduceForFarmerRow struct {
	CropName     string    `json:"crop_name"`
	CropID       int64     `json:"crop_id"`
	Crophscode   string    `json:"crophscode"`
	Riskstatus   string    `json:"riskstatus"`
	CicCode      string    `json:"cic_code"`
	Plantingdate time.Time `json:"plantingdate"`
	Acreage      float64   `json:"acreage"`
}

func (q *Queries) GetProduceForFarmer(ctx context.Context, farmerID int32) ([]GetProduceForFarmerRow, error) {
	rows, err := q.db.QueryContext(ctx, getProduceForFarmer, farmerID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetProduceForFarmerRow{}
	for rows.Next() {
		var i GetProduceForFarmerRow
		if err := rows.Scan(
			&i.CropName,
			&i.CropID,
			&i.Crophscode,
			&i.Riskstatus,
			&i.CicCode,
			&i.Plantingdate,
			&i.Acreage,
		); 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 listFarmers = `-- name: ListFarmers :many
SELECT farmers.fmcode,farmers.id farmer_id,farmers.fmid,farmers.fmidtype,farmers.fmname,farmers.fmphone,farmers.created_at farmer_created_at, farmers.status,
farmers.fmemail from farmers
ORDER BY farmers.id
LIMIT $1
OFFSET $2
`

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

type ListFarmersRow struct {
	Fmcode          sql.NullString `json:"fmcode"`
	FarmerID        int64          `json:"farmer_id"`
	Fmid            string         `json:"fmid"`
	Fmidtype        string         `json:"fmidtype"`
	Fmname          string         `json:"fmname"`
	Fmphone         string         `json:"fmphone"`
	FarmerCreatedAt time.Time      `json:"farmer_created_at"`
	Status          string         `json:"status"`
	Fmemail         sql.NullString `json:"fmemail"`
}

func (q *Queries) ListFarmers(ctx context.Context, arg ListFarmersParams) ([]ListFarmersRow, error) {
	rows, err := q.db.QueryContext(ctx, listFarmers, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListFarmersRow{}
	for rows.Next() {
		var i ListFarmersRow
		if err := rows.Scan(
			&i.Fmcode,
			&i.FarmerID,
			&i.Fmid,
			&i.Fmidtype,
			&i.Fmname,
			&i.Fmphone,
			&i.FarmerCreatedAt,
			&i.Status,
			&i.Fmemail,
		); 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 listFarmersForExporters = `-- name: ListFarmersForExporters :many
SELECT farmers.fmcode,farmers.id farmer_id,farmers.fmid,farmers.status farmer_status,farmers.fmidtype,farmers.fmname,farmers.fmphone,farmers.created_at farmer_created_at, farmers.status,
farmers.fmemail FROM farmers
INNER JOIN farmer_exporter on farmer_exporter.farmer_id = farmers.id
WHERE farmer_exporter.exportcompany_id = $3
AND   farmers.status = $4
ORDER BY farmers.id DESC
LIMIT $1 OFFSET $2
`

type ListFarmersForExportersParams struct {
	Limit           int32  `json:"limit"`
	Offset          int32  `json:"offset"`
	ExportcompanyID int32  `json:"exportcompany_id"`
	Status          string `json:"status"`
}

type ListFarmersForExportersRow struct {
	Fmcode          sql.NullString `json:"fmcode"`
	FarmerID        int64          `json:"farmer_id"`
	Fmid            string         `json:"fmid"`
	FarmerStatus    string         `json:"farmer_status"`
	Fmidtype        string         `json:"fmidtype"`
	Fmname          string         `json:"fmname"`
	Fmphone         string         `json:"fmphone"`
	FarmerCreatedAt time.Time      `json:"farmer_created_at"`
	Status          string         `json:"status"`
	Fmemail         sql.NullString `json:"fmemail"`
}

func (q *Queries) ListFarmersForExporters(ctx context.Context, arg ListFarmersForExportersParams) ([]ListFarmersForExportersRow, error) {
	rows, err := q.db.QueryContext(ctx, listFarmersForExporters,
		arg.Limit,
		arg.Offset,
		arg.ExportcompanyID,
		arg.Status,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListFarmersForExportersRow{}
	for rows.Next() {
		var i ListFarmersForExportersRow
		if err := rows.Scan(
			&i.Fmcode,
			&i.FarmerID,
			&i.Fmid,
			&i.FarmerStatus,
			&i.Fmidtype,
			&i.Fmname,
			&i.Fmphone,
			&i.FarmerCreatedAt,
			&i.Status,
			&i.Fmemail,
		); 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 searchFarmerName = `-- name: SearchFarmerName :many
SELECT id, fmcode, fmid, fmidtype, fmname, fmphoto, fmphone, fmemail, status, isverified, created_at from farmers
WHERE LOWER(fmname) LIKE '%' || LOWER($1) || '%'
`

func (q *Queries) SearchFarmerName(ctx context.Context, lower string) ([]Farmer, error) {
	rows, err := q.db.QueryContext(ctx, searchFarmerName, lower)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Farmer{}
	for rows.Next() {
		var i Farmer
		if err := rows.Scan(
			&i.ID,
			&i.Fmcode,
			&i.Fmid,
			&i.Fmidtype,
			&i.Fmname,
			&i.Fmphoto,
			&i.Fmphone,
			&i.Fmemail,
			&i.Status,
			&i.Isverified,
			&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 searchTraceabilityCodes = `-- name: SearchTraceabilityCodes :many
SELECT
 farmer_farms.traceability_code,
 redflags.status,
 farmer_farms.acreage,
 farmer_farms.harvestbalance,
 crops.cropname,
 farmers.fmname
FROM
 farmer_farms
LEFT JOIN
 crops ON crops.id = farmer_farms.crop_id
LEFT JOIN
 redflags ON redflags.traceability_code = farmer_farms.traceability_code
LEFT JOIN
 farmers ON farmers.id = farmer_farms.farmer_id
WHERE
 farmer_farms.traceability_code LIKE '%' || $1 || '%'
`

type SearchTraceabilityCodesRow struct {
	TraceabilityCode string          `json:"traceability_code"`
	Status           sql.NullString  `json:"status"`
	Acreage          float64         `json:"acreage"`
	Harvestbalance   sql.NullFloat64 `json:"harvestbalance"`
	Cropname         sql.NullString  `json:"cropname"`
	Fmname           sql.NullString  `json:"fmname"`
}

func (q *Queries) SearchTraceabilityCodes(ctx context.Context, dollar_1 sql.NullString) ([]SearchTraceabilityCodesRow, error) {
	rows, err := q.db.QueryContext(ctx, searchTraceabilityCodes, dollar_1)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []SearchTraceabilityCodesRow{}
	for rows.Next() {
		var i SearchTraceabilityCodesRow
		if err := rows.Scan(
			&i.TraceabilityCode,
			&i.Status,
			&i.Acreage,
			&i.Harvestbalance,
			&i.Cropname,
			&i.Fmname,
		); 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 updateFarmer = `-- name: UpdateFarmer :one
UPDATE farmers
  set
    fmcode=$2,
    fmname=$3,
    fmphone=$4,
    fmemail=$5,
    fmid= $6,
    fmidtype = $7
WHERE id = $1
RETURNING id, fmcode, fmid, fmidtype, fmname, fmphoto, fmphone, fmemail, status, isverified, created_at
`

type UpdateFarmerParams struct {
	ID       int64          `json:"id"`
	Fmcode   sql.NullString `json:"fmcode"`
	Fmname   string         `json:"fmname"`
	Fmphone  string         `json:"fmphone"`
	Fmemail  sql.NullString `json:"fmemail"`
	Fmid     string         `json:"fmid"`
	Fmidtype string         `json:"fmidtype"`
}

func (q *Queries) UpdateFarmer(ctx context.Context, arg UpdateFarmerParams) (Farmer, error) {
	row := q.db.QueryRowContext(ctx, updateFarmer,
		arg.ID,
		arg.Fmcode,
		arg.Fmname,
		arg.Fmphone,
		arg.Fmemail,
		arg.Fmid,
		arg.Fmidtype,
	)
	var i Farmer
	err := row.Scan(
		&i.ID,
		&i.Fmcode,
		&i.Fmid,
		&i.Fmidtype,
		&i.Fmname,
		&i.Fmphoto,
		&i.Fmphone,
		&i.Fmemail,
		&i.Status,
		&i.Isverified,
		&i.CreatedAt,
	)
	return i, err
}

const updateFarmerFarmHarvest = `-- name: UpdateFarmerFarmHarvest :one
UPDATE farmer_farms
  set
    harvestbalance = $3
WHERE  crop_id = $2
AND    farmer_id = $1
RETURNING id, farmer_id, crop_id, traceability_code, harvestbalance, district_id, subcounty_id, parish_id, village_id, acreage, lat, long, created_at, plantingdate, status
`

type UpdateFarmerFarmHarvestParams struct {
	FarmerID       int32           `json:"farmer_id"`
	CropID         int32           `json:"crop_id"`
	Harvestbalance sql.NullFloat64 `json:"harvestbalance"`
}

func (q *Queries) UpdateFarmerFarmHarvest(ctx context.Context, arg UpdateFarmerFarmHarvestParams) (FarmerFarm, error) {
	row := q.db.QueryRowContext(ctx, updateFarmerFarmHarvest, arg.FarmerID, arg.CropID, arg.Harvestbalance)
	var i FarmerFarm
	err := row.Scan(
		&i.ID,
		&i.FarmerID,
		&i.CropID,
		&i.TraceabilityCode,
		&i.Harvestbalance,
		&i.DistrictID,
		&i.SubcountyID,
		&i.ParishID,
		&i.VillageID,
		&i.Acreage,
		&i.Lat,
		&i.Long,
		&i.CreatedAt,
		&i.Plantingdate,
		&i.Status,
	)
	return i, err
}

const updateFarmerFarmHarvestBalance = `-- name: UpdateFarmerFarmHarvestBalance :one
UPDATE farmer_farms
  set
    harvestbalance = $2
WHERE traceability_code = $1
RETURNING id, farmer_id, crop_id, traceability_code, harvestbalance, district_id, subcounty_id, parish_id, village_id, acreage, lat, long, created_at, plantingdate, status
`

type UpdateFarmerFarmHarvestBalanceParams struct {
	TraceabilityCode string          `json:"traceability_code"`
	Harvestbalance   sql.NullFloat64 `json:"harvestbalance"`
}

func (q *Queries) UpdateFarmerFarmHarvestBalance(ctx context.Context, arg UpdateFarmerFarmHarvestBalanceParams) (FarmerFarm, error) {
	row := q.db.QueryRowContext(ctx, updateFarmerFarmHarvestBalance, arg.TraceabilityCode, arg.Harvestbalance)
	var i FarmerFarm
	err := row.Scan(
		&i.ID,
		&i.FarmerID,
		&i.CropID,
		&i.TraceabilityCode,
		&i.Harvestbalance,
		&i.DistrictID,
		&i.SubcountyID,
		&i.ParishID,
		&i.VillageID,
		&i.Acreage,
		&i.Lat,
		&i.Long,
		&i.CreatedAt,
		&i.Plantingdate,
		&i.Status,
	)
	return i, err
}

const updateSingleFarmerFarms = `-- name: UpdateSingleFarmerFarms :one
UPDATE farmer_farms
    set
    traceability_code = $2,
    district_id = $3,
    crop_id = $4,
    parish_id = $5,
    village_id = $6,
    subcounty_id = $7,
    acreage = $8,
    lat = $9,
    long = $10
    WHERE id = $1
RETURNING id, farmer_id, crop_id, traceability_code, harvestbalance, district_id, subcounty_id, parish_id, village_id, acreage, lat, long, created_at, plantingdate, status
`

type UpdateSingleFarmerFarmsParams struct {
	ID               int64   `json:"id"`
	TraceabilityCode string  `json:"traceability_code"`
	DistrictID       int32   `json:"district_id"`
	CropID           int32   `json:"crop_id"`
	ParishID         int32   `json:"parish_id"`
	VillageID        int32   `json:"village_id"`
	SubcountyID      int32   `json:"subcounty_id"`
	Acreage          float64 `json:"acreage"`
	Lat              float64 `json:"lat"`
	Long             float64 `json:"long"`
}

func (q *Queries) UpdateSingleFarmerFarms(ctx context.Context, arg UpdateSingleFarmerFarmsParams) (FarmerFarm, error) {
	row := q.db.QueryRowContext(ctx, updateSingleFarmerFarms,
		arg.ID,
		arg.TraceabilityCode,
		arg.DistrictID,
		arg.CropID,
		arg.ParishID,
		arg.VillageID,
		arg.SubcountyID,
		arg.Acreage,
		arg.Lat,
		arg.Long,
	)
	var i FarmerFarm
	err := row.Scan(
		&i.ID,
		&i.FarmerID,
		&i.CropID,
		&i.TraceabilityCode,
		&i.Harvestbalance,
		&i.DistrictID,
		&i.SubcountyID,
		&i.ParishID,
		&i.VillageID,
		&i.Acreage,
		&i.Lat,
		&i.Long,
		&i.CreatedAt,
		&i.Plantingdate,
		&i.Status,
	)
	return i, err
}

const uploadFarmerPhoto = `-- name: UploadFarmerPhoto :one
UPDATE farmers
     SET
       fmphoto = $2
WHERE id = $1
RETURNING id, fmcode, fmid, fmidtype, fmname, fmphoto, fmphone, fmemail, status, isverified, created_at
`

type UploadFarmerPhotoParams struct {
	ID      int64          `json:"id"`
	Fmphoto sql.NullString `json:"fmphoto"`
}

func (q *Queries) UploadFarmerPhoto(ctx context.Context, arg UploadFarmerPhotoParams) (Farmer, error) {
	row := q.db.QueryRowContext(ctx, uploadFarmerPhoto, arg.ID, arg.Fmphoto)
	var i Farmer
	err := row.Scan(
		&i.ID,
		&i.Fmcode,
		&i.Fmid,
		&i.Fmidtype,
		&i.Fmname,
		&i.Fmphoto,
		&i.Fmphone,
		&i.Fmemail,
		&i.Status,
		&i.Isverified,
		&i.CreatedAt,
	)
	return i, err
}
