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

package db

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

	"github.com/google/uuid"
)

const assignPackhouseInspectorToExporters = `-- name: AssignPackhouseInspectorToExporters :one
INSERT INTO user_exporter(
    "user_id",
    "exportcompany_id"
 ) VALUES (
     $1,
     $2
  )
RETURNING id, user_id, exportcompany_id, created_at
`

type AssignPackhouseInspectorToExportersParams struct {
	UserID          int32 `json:"user_id"`
	ExportcompanyID int32 `json:"exportcompany_id"`
}

func (q *Queries) AssignPackhouseInspectorToExporters(ctx context.Context, arg AssignPackhouseInspectorToExportersParams) (UserExporter, error) {
	row := q.db.QueryRowContext(ctx, assignPackhouseInspectorToExporters, arg.UserID, arg.ExportcompanyID)
	var i UserExporter
	err := row.Scan(
		&i.ID,
		&i.UserID,
		&i.ExportcompanyID,
		&i.CreatedAt,
	)
	return i, err
}

const attachUserExpoter = `-- name: AttachUserExpoter :exec
INSERT INTO user_exporter (
    user_id,
    exportcompany_id
 ) VALUES (
    $1, $2
 )
`

type AttachUserExpoterParams struct {
	UserID          int32 `json:"user_id"`
	ExportcompanyID int32 `json:"exportcompany_id"`
}

func (q *Queries) AttachUserExpoter(ctx context.Context, arg AttachUserExpoterParams) error {
	_, err := q.db.ExecContext(ctx, attachUserExpoter, arg.UserID, arg.ExportcompanyID)
	return err
}

const attachUserPackhouse = `-- name: AttachUserPackhouse :exec
INSERT INTO user_packhouse (
    user_id,
    packhouse_id
 ) VALUES (
    $1, $2
 )
`

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

func (q *Queries) AttachUserPackhouse(ctx context.Context, arg AttachUserPackhouseParams) error {
	_, err := q.db.ExecContext(ctx, attachUserPackhouse, arg.UserID, arg.PackhouseID)
	return err
}

const attachUserZones = `-- name: AttachUserZones :exec
INSERT INTO user_zones (
    user_id,
    zone_id
 ) VALUES (
    $1, $2
 )
`

type AttachUserZonesParams struct {
	UserID int32 `json:"user_id"`
	ZoneID int32 `json:"zone_id"`
}

func (q *Queries) AttachUserZones(ctx context.Context, arg AttachUserZonesParams) error {
	_, err := q.db.ExecContext(ctx, attachUserZones, arg.UserID, arg.ZoneID)
	return err
}

const borderInspectorCount = `-- name: BorderInspectorCount :one
SELECT COUNT(id)
FROM  users
WHERE user_type = 'BORDER-INSPECTOR'
`

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

const checkEmailExistance = `-- name: CheckEmailExistance :one
SELECT EXISTS (
   SELECT 1 from users
   WHERE email = $1
)
`

func (q *Queries) CheckEmailExistance(ctx context.Context, email string) (bool, error) {
	row := q.db.QueryRowContext(ctx, checkEmailExistance, email)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const checkQcExporterExistance = `-- name: CheckQcExporterExistance :one
SELECT EXISTS (
   SELECT 1 from user_exporter
   WHERE exportcompany_id = $1
   AND   user_id = $2
)
`

type CheckQcExporterExistanceParams struct {
	ExportcompanyID int32 `json:"exportcompany_id"`
	UserID          int32 `json:"user_id"`
}

func (q *Queries) CheckQcExporterExistance(ctx context.Context, arg CheckQcExporterExistanceParams) (bool, error) {
	row := q.db.QueryRowContext(ctx, checkQcExporterExistance, arg.ExportcompanyID, arg.UserID)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const checkUserExistance = `-- name: CheckUserExistance :one
SELECT EXISTS (
    SELECT 1
    FROM  user_exporter
    WHERE user_id = $1
    AND   exportcompany_id = $2
) AS exists
`

type CheckUserExistanceParams struct {
	UserID          int32 `json:"user_id"`
	ExportcompanyID int32 `json:"exportcompany_id"`
}

func (q *Queries) CheckUserExistance(ctx context.Context, arg CheckUserExistanceParams) (bool, error) {
	row := q.db.QueryRowContext(ctx, checkUserExistance, arg.UserID, arg.ExportcompanyID)
	var exists bool
	err := row.Scan(&exists)
	return exists, err
}

const countGlobalUsers = `-- name: CountGlobalUsers :one
Select COUNT(DISTINCT id) AS unique_users from users
`

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

const createUser = `-- name: CreateUser :one
INSERT INTO users (
    fullname,
    nin,
    regno,
    phone,
    user_type,
    stamp,
    signature,
    email,
    password
 ) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8, $9
 ) RETURNING id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at
`

type CreateUserParams struct {
	Fullname  string         `json:"fullname"`
	Nin       sql.NullString `json:"nin"`
	Regno     sql.NullString `json:"regno"`
	Phone     string         `json:"phone"`
	UserType  string         `json:"user_type"`
	Stamp     sql.NullString `json:"stamp"`
	Signature sql.NullString `json:"signature"`
	Email     string         `json:"email"`
	Password  string         `json:"password"`
}

func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
	row := q.db.QueryRowContext(ctx, createUser,
		arg.Fullname,
		arg.Nin,
		arg.Regno,
		arg.Phone,
		arg.UserType,
		arg.Stamp,
		arg.Signature,
		arg.Email,
		arg.Password,
	)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const createUserLogs = `-- name: CreateUserLogs :exec
INSERT INTO userlogs (
     id,
     user_id,
     activity_type,
     activity_description,
     usertype,
     organisation,
     organisationid,
     ip_address,
     user_agent
 ) VALUES (
    $1,
    $2,
    $3,
    $4,
    $5,
    $6,
    $7,
    $8,
    $9
 ) RETURNING id, user_id, activity_type, activity_description, ip_address, user_agent, created_at, organisation, organisationid, usertype, status
`

type CreateUserLogsParams struct {
	ID                  uuid.UUID      `json:"id"`
	UserID              int32          `json:"user_id"`
	ActivityType        string         `json:"activity_type"`
	ActivityDescription string         `json:"activity_description"`
	Usertype            sql.NullString `json:"usertype"`
	Organisation        sql.NullString `json:"organisation"`
	Organisationid      sql.NullInt32  `json:"organisationid"`
	IpAddress           string         `json:"ip_address"`
	UserAgent           string         `json:"user_agent"`
}

func (q *Queries) CreateUserLogs(ctx context.Context, arg CreateUserLogsParams) error {
	_, err := q.db.ExecContext(ctx, createUserLogs,
		arg.ID,
		arg.UserID,
		arg.ActivityType,
		arg.ActivityDescription,
		arg.Usertype,
		arg.Organisation,
		arg.Organisationid,
		arg.IpAddress,
		arg.UserAgent,
	)
	return err
}

const dICICCount = `-- name: DICICCount :one
SELECT COUNT(id)
FROM users
WHERE user_type = 'DCIC-ADMIN'
`

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

const deleteUser = `-- name: DeleteUser :exec
DELETE FROM users WHERE id= $1
`

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

const detachPackhouseInspectorFromCompany = `-- name: DetachPackhouseInspectorFromCompany :exec
DELETE FROM user_exporter where
exportcompany_id = $1 AND user_id = $2
`

type DetachPackhouseInspectorFromCompanyParams struct {
	ExportcompanyID int32 `json:"exportcompany_id"`
	UserID          int32 `json:"user_id"`
}

func (q *Queries) DetachPackhouseInspectorFromCompany(ctx context.Context, arg DetachPackhouseInspectorFromCompanyParams) error {
	_, err := q.db.ExecContext(ctx, detachPackhouseInspectorFromCompany, arg.ExportcompanyID, arg.UserID)
	return err
}

const exporterAdminManagerCount = `-- name: ExporterAdminManagerCount :one
SELECT COUNT(users.id)
FROM  users
LEFT JOIN user_exporter on user_exporter.user_id = users.id
WHERE users.user_type IN ('MANAGER', 'EXPORTER')
AND   user_exporter.exportcompany_id = $1
`

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

const exporterCount = `-- name: ExporterCount :one
SELECT COUNT(id)
FROM users
WHERE user_type = 'EXPORTER'
`

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

const fetchAllxporterLogs = `-- name: FetchAllxporterLogs :many
SELECT userlogs.id,users.fullname,
userlogs.user_id,userlogs.activity_type,
userlogs.activity_description,userlogs.usertype,
userlogs.organisation,userlogs.organisationid,
userlogs.ip_address,userlogs.created_at,userlogs.status,userlogs.user_agent from userlogs
JOIN  users on users.id = userlogs.user_id
WHERE organisationid = $1 LIMIT $2 OFFSET $3
`

type FetchAllxporterLogsParams struct {
	Organisationid sql.NullInt32 `json:"organisationid"`
	Limit          int32         `json:"limit"`
	Offset         int32         `json:"offset"`
}

type FetchAllxporterLogsRow struct {
	ID                  uuid.UUID      `json:"id"`
	Fullname            string         `json:"fullname"`
	UserID              int32          `json:"user_id"`
	ActivityType        string         `json:"activity_type"`
	ActivityDescription string         `json:"activity_description"`
	Usertype            sql.NullString `json:"usertype"`
	Organisation        sql.NullString `json:"organisation"`
	Organisationid      sql.NullInt32  `json:"organisationid"`
	IpAddress           string         `json:"ip_address"`
	CreatedAt           time.Time      `json:"created_at"`
	Status              sql.NullString `json:"status"`
	UserAgent           string         `json:"user_agent"`
}

func (q *Queries) FetchAllxporterLogs(ctx context.Context, arg FetchAllxporterLogsParams) ([]FetchAllxporterLogsRow, error) {
	rows, err := q.db.QueryContext(ctx, fetchAllxporterLogs, arg.Organisationid, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []FetchAllxporterLogsRow{}
	for rows.Next() {
		var i FetchAllxporterLogsRow
		if err := rows.Scan(
			&i.ID,
			&i.Fullname,
			&i.UserID,
			&i.ActivityType,
			&i.ActivityDescription,
			&i.Usertype,
			&i.Organisation,
			&i.Organisationid,
			&i.IpAddress,
			&i.CreatedAt,
			&i.Status,
			&i.UserAgent,
		); 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 fetchDistinctType = `-- name: FetchDistinctType :many
SELECT DISTINCT activity_type
FROM userlogs
`

func (q *Queries) FetchDistinctType(ctx context.Context) ([]string, error) {
	rows, err := q.db.QueryContext(ctx, fetchDistinctType)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []string{}
	for rows.Next() {
		var activity_type string
		if err := rows.Scan(&activity_type); err != nil {
			return nil, err
		}
		items = append(items, activity_type)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const fetchExporterStamp = `-- name: FetchExporterStamp :one
SELECT users.stamp FROM users
INNER  JOIN user_exporter on user_exporter.user_id = users.id
WHERE  users.user_type = 'EXPORTER'
AND    user_exporter.exportcompany_id = $1
`

func (q *Queries) FetchExporterStamp(ctx context.Context, exportcompanyID int32) (sql.NullString, error) {
	row := q.db.QueryRowContext(ctx, fetchExporterStamp, exportcompanyID)
	var stamp sql.NullString
	err := row.Scan(&stamp)
	return stamp, err
}

const getAllDCICAdmins = `-- name: GetAllDCICAdmins :many
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at from users
where user_type = 'DCIC-ADMIN'
`

func (q *Queries) GetAllDCICAdmins(ctx context.Context) ([]User, error) {
	rows, err := q.db.QueryContext(ctx, getAllDCICAdmins)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []User{}
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Stamp,
			&i.Signature,
			&i.Phone,
			&i.Status,
			&i.UserType,
			&i.Email,
			&i.Password,
			&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 getAllUsers = `-- name: GetAllUsers :many
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at from users
`

func (q *Queries) GetAllUsers(ctx context.Context) ([]User, error) {
	rows, err := q.db.QueryContext(ctx, getAllUsers)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []User{}
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Stamp,
			&i.Signature,
			&i.Phone,
			&i.Status,
			&i.UserType,
			&i.Email,
			&i.Password,
			&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 getDICUsers = `-- name: GetDICUsers :many
SELECT users.phone,users.fullname,users.email from users
WHERE users.user_type = 'DCIC-ADMIN'
`

type GetDICUsersRow struct {
	Phone    string `json:"phone"`
	Fullname string `json:"fullname"`
	Email    string `json:"email"`
}

func (q *Queries) GetDICUsers(ctx context.Context) ([]GetDICUsersRow, error) {
	rows, err := q.db.QueryContext(ctx, getDICUsers)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetDICUsersRow{}
	for rows.Next() {
		var i GetDICUsersRow
		if err := rows.Scan(&i.Phone, &i.Fullname, &i.Email); 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 getFetchUserByUserType = `-- name: GetFetchUserByUserType :many
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at from users where users.user_type = $1
`

func (q *Queries) GetFetchUserByUserType(ctx context.Context, userType string) ([]User, error) {
	rows, err := q.db.QueryContext(ctx, getFetchUserByUserType, userType)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []User{}
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Stamp,
			&i.Signature,
			&i.Phone,
			&i.Status,
			&i.UserType,
			&i.Email,
			&i.Password,
			&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 getFilteredLogs = `-- name: GetFilteredLogs :many
SELECT userlogs.id,users.fullname,
userlogs.user_id,userlogs.activity_type,
userlogs.activity_description,userlogs.usertype,
userlogs.organisation,userlogs.organisationid,
userlogs.ip_address,userlogs.created_at,userlogs.status,userlogs.user_agent FROM userlogs
JOIN  users on users.id = userlogs.user_id
WHERE (COALESCE($1, '') = '' OR usertype = $1)
AND   (COALESCE($2::integer, 0) = 0 OR organisationid = $2::integer)
AND   (COALESCE($3, '') = '' OR activity_type = $3)
ORDER BY userlogs.created_at DESC LIMIT $4 OFFSET $5
`

type GetFilteredLogsParams struct {
	Column1 interface{} `json:"column_1"`
	Column2 int32       `json:"column_2"`
	Column3 interface{} `json:"column_3"`
	Limit   int32       `json:"limit"`
	Offset  int32       `json:"offset"`
}

type GetFilteredLogsRow struct {
	ID                  uuid.UUID      `json:"id"`
	Fullname            string         `json:"fullname"`
	UserID              int32          `json:"user_id"`
	ActivityType        string         `json:"activity_type"`
	ActivityDescription string         `json:"activity_description"`
	Usertype            sql.NullString `json:"usertype"`
	Organisation        sql.NullString `json:"organisation"`
	Organisationid      sql.NullInt32  `json:"organisationid"`
	IpAddress           string         `json:"ip_address"`
	CreatedAt           time.Time      `json:"created_at"`
	Status              sql.NullString `json:"status"`
	UserAgent           string         `json:"user_agent"`
}

func (q *Queries) GetFilteredLogs(ctx context.Context, arg GetFilteredLogsParams) ([]GetFilteredLogsRow, error) {
	rows, err := q.db.QueryContext(ctx, getFilteredLogs,
		arg.Column1,
		arg.Column2,
		arg.Column3,
		arg.Limit,
		arg.Offset,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetFilteredLogsRow{}
	for rows.Next() {
		var i GetFilteredLogsRow
		if err := rows.Scan(
			&i.ID,
			&i.Fullname,
			&i.UserID,
			&i.ActivityType,
			&i.ActivityDescription,
			&i.Usertype,
			&i.Organisation,
			&i.Organisationid,
			&i.IpAddress,
			&i.CreatedAt,
			&i.Status,
			&i.UserAgent,
		); 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 getInspector = `-- name: GetInspector :one
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at FROM users
WHERE id= $1
`

func (q *Queries) GetInspector(ctx context.Context, id int64) (User, error) {
	row := q.db.QueryRowContext(ctx, getInspector, id)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const getPackHouseInspectorCompanies = `-- name: GetPackHouseInspectorCompanies :many
SELECT exportcompanies.expname,exportcompanies.expregno,packhouses.phname,packhouses.id as packhouse_id,packhouses.created_at as packhouse_creation_date,
exportcompanies.created_at as exportcompanies_creation_date,user_exporter.created_at as assignment_date,
exportcompanies.id as exportcompany_id,packhouses.phregno,exportcompanies.expaddress1,exportcompanies.expcompanyphone,exportcompanies.expemail,
exportcompanies.registrationstatus,exportcompanies.validitydate from user_exporter
JOIN   users on users.id = user_exporter.user_id
JOIN   exportcompanies on exportcompanies.id = user_exporter.exportcompany_id
JOIN   packhouse_exporter on packhouse_exporter.exportcompany_id = exportcompanies.id
JOIN   packhouses on packhouses.id  = packhouse_exporter.packhouse_id
WHERE  users.user_type='PACKHOUSE-INSPECTOR' AND users.id = $1
`

type GetPackHouseInspectorCompaniesRow struct {
	Expname                     string    `json:"expname"`
	Expregno                    string    `json:"expregno"`
	Phname                      string    `json:"phname"`
	PackhouseID                 int64     `json:"packhouse_id"`
	PackhouseCreationDate       time.Time `json:"packhouse_creation_date"`
	ExportcompaniesCreationDate time.Time `json:"exportcompanies_creation_date"`
	AssignmentDate              time.Time `json:"assignment_date"`
	ExportcompanyID             int64     `json:"exportcompany_id"`
	Phregno                     string    `json:"phregno"`
	Expaddress1                 string    `json:"expaddress1"`
	Expcompanyphone             string    `json:"expcompanyphone"`
	Expemail                    string    `json:"expemail"`
	Registrationstatus          string    `json:"registrationstatus"`
	Validitydate                time.Time `json:"validitydate"`
}

func (q *Queries) GetPackHouseInspectorCompanies(ctx context.Context, id int64) ([]GetPackHouseInspectorCompaniesRow, error) {
	rows, err := q.db.QueryContext(ctx, getPackHouseInspectorCompanies, id)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []GetPackHouseInspectorCompaniesRow{}
	for rows.Next() {
		var i GetPackHouseInspectorCompaniesRow
		if err := rows.Scan(
			&i.Expname,
			&i.Expregno,
			&i.Phname,
			&i.PackhouseID,
			&i.PackhouseCreationDate,
			&i.ExportcompaniesCreationDate,
			&i.AssignmentDate,
			&i.ExportcompanyID,
			&i.Phregno,
			&i.Expaddress1,
			&i.Expcompanyphone,
			&i.Expemail,
			&i.Registrationstatus,
			&i.Validitydate,
		); 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 getPackhouseID = `-- name: GetPackhouseID :one
 SELECT packhouse_id from packhouse_exporter
 WHERE exportcompany_id = $1
`

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

const getPackhouseInspectorByID = `-- name: GetPackhouseInspectorByID :one
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at FROM users
WHERE user_type IN ('PACKHOUSE-INSPECTOR','ZONAL-PACKHOUSE-INSPECTOR')
AND id = $1
`

func (q *Queries) GetPackhouseInspectorByID(ctx context.Context, id int64) (User, error) {
	row := q.db.QueryRowContext(ctx, getPackhouseInspectorByID, id)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const getQCCompany = `-- name: GetQCCompany :one
SELECT exportcompanies.expname from user_exporter
JOIN   users           on users.id           = user_exporter.user_id
JOIN   exportcompanies on exportcompanies.id = user_exporter.exportcompany_id
WHERE  users.user_type = 'QUALITY-CONTROLLER' AND users.id = $1
LIMIT  1
`

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

const getTodayUserLogsByUserType = `-- name: GetTodayUserLogsByUserType :one
SELECT COUNT(DISTINCT user_id) AS unique_users
FROM userlogs
WHERE
  created_at >= CURRENT_DATE 
  AND usertype = $1
`

func (q *Queries) GetTodayUserLogsByUserType(ctx context.Context, usertype sql.NullString) (int64, error) {
	row := q.db.QueryRowContext(ctx, getTodayUserLogsByUserType, usertype)
	var unique_users int64
	err := row.Scan(&unique_users)
	return unique_users, err
}

const getTodaysLogs = `-- name: GetTodaysLogs :many
SELECT id, user_id, activity_type, activity_description, ip_address, user_agent, created_at, organisation, organisationid, usertype, status 
FROM userlogs
`

func (q *Queries) GetTodaysLogs(ctx context.Context) ([]Userlog, error) {
	rows, err := q.db.QueryContext(ctx, getTodaysLogs)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []Userlog{}
	for rows.Next() {
		var i Userlog
		if err := rows.Scan(
			&i.ID,
			&i.UserID,
			&i.ActivityType,
			&i.ActivityDescription,
			&i.IpAddress,
			&i.UserAgent,
			&i.CreatedAt,
			&i.Organisation,
			&i.Organisationid,
			&i.Usertype,
			&i.Status,
		); 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 getUser = `-- name: GetUser :one
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at FROM users
WHERE id = $1 LIMIT 1
`

func (q *Queries) GetUser(ctx context.Context, id int64) (User, error) {
	row := q.db.QueryRowContext(ctx, getUser, id)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const getUserByEmail = `-- name: GetUserByEmail :one
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at FROM users
WHERE email = $1 LIMIT 1
`

func (q *Queries) GetUserByEmail(ctx context.Context, email string) (User, error) {
	row := q.db.QueryRowContext(ctx, getUserByEmail, email)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const listDcicAdmins = `-- name: ListDcicAdmins :many
SELECT users.id as user_id, fullname,nin,regno,phone,email,status,created_at FROM users
WHERE user_type = $3
ORDER BY id LIMIT $1
OFFSET $2
`

type ListDcicAdminsParams struct {
	Limit    int32  `json:"limit"`
	Offset   int32  `json:"offset"`
	UserType string `json:"user_type"`
}

type ListDcicAdminsRow struct {
	UserID    int64          `json:"user_id"`
	Fullname  string         `json:"fullname"`
	Nin       sql.NullString `json:"nin"`
	Regno     sql.NullString `json:"regno"`
	Phone     string         `json:"phone"`
	Email     string         `json:"email"`
	Status    sql.NullString `json:"status"`
	CreatedAt time.Time      `json:"created_at"`
}

func (q *Queries) ListDcicAdmins(ctx context.Context, arg ListDcicAdminsParams) ([]ListDcicAdminsRow, error) {
	rows, err := q.db.QueryContext(ctx, listDcicAdmins, arg.Limit, arg.Offset, arg.UserType)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListDcicAdminsRow{}
	for rows.Next() {
		var i ListDcicAdminsRow
		if err := rows.Scan(
			&i.UserID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Phone,
			&i.Email,
			&i.Status,
			&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 listInspectors = `-- name: ListInspectors :many
SELECT users.id as user_id, fullname,nin,regno,phone,email,user_type FROM user_exporter
LEFT JOIN users ON users.id = user_exporter.user_id
WHERE users.user_type = $3 AND user_exporter.exportcompany_id= $4
LIMIT $1
OFFSET $2
`

type ListInspectorsParams struct {
	Limit           int32  `json:"limit"`
	Offset          int32  `json:"offset"`
	UserType        string `json:"user_type"`
	ExportcompanyID int32  `json:"exportcompany_id"`
}

type ListInspectorsRow struct {
	UserID   sql.NullInt64  `json:"user_id"`
	Fullname sql.NullString `json:"fullname"`
	Nin      sql.NullString `json:"nin"`
	Regno    sql.NullString `json:"regno"`
	Phone    sql.NullString `json:"phone"`
	Email    sql.NullString `json:"email"`
	UserType sql.NullString `json:"user_type"`
}

func (q *Queries) ListInspectors(ctx context.Context, arg ListInspectorsParams) ([]ListInspectorsRow, error) {
	rows, err := q.db.QueryContext(ctx, listInspectors,
		arg.Limit,
		arg.Offset,
		arg.UserType,
		arg.ExportcompanyID,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListInspectorsRow{}
	for rows.Next() {
		var i ListInspectorsRow
		if err := rows.Scan(
			&i.UserID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Phone,
			&i.Email,
			&i.UserType,
		); 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 listQualityControllers = `-- name: ListQualityControllers :many
SELECT users.id as user_id, fullname,nin,regno,phone,email,user_type FROM user_exporter
LEFT JOIN users ON users.id = user_exporter.user_id
WHERE user_exporter.exportcompany_id= $3
LIMIT $1
OFFSET $2
`

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

type ListQualityControllersRow struct {
	UserID   sql.NullInt64  `json:"user_id"`
	Fullname sql.NullString `json:"fullname"`
	Nin      sql.NullString `json:"nin"`
	Regno    sql.NullString `json:"regno"`
	Phone    sql.NullString `json:"phone"`
	Email    sql.NullString `json:"email"`
	UserType sql.NullString `json:"user_type"`
}

func (q *Queries) ListQualityControllers(ctx context.Context, arg ListQualityControllersParams) ([]ListQualityControllersRow, error) {
	rows, err := q.db.QueryContext(ctx, listQualityControllers, arg.Limit, arg.Offset, arg.ExportcompanyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListQualityControllersRow{}
	for rows.Next() {
		var i ListQualityControllersRow
		if err := rows.Scan(
			&i.UserID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Phone,
			&i.Email,
			&i.UserType,
		); 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 listSuperAdmins = `-- name: ListSuperAdmins :many
SELECT users.id as user_id, fullname,nin,regno,phone,email,status,created_at FROM users
WHERE user_type = $3
ORDER BY id LIMIT $1
OFFSET $2
`

type ListSuperAdminsParams struct {
	Limit    int32  `json:"limit"`
	Offset   int32  `json:"offset"`
	UserType string `json:"user_type"`
}

type ListSuperAdminsRow struct {
	UserID    int64          `json:"user_id"`
	Fullname  string         `json:"fullname"`
	Nin       sql.NullString `json:"nin"`
	Regno     sql.NullString `json:"regno"`
	Phone     string         `json:"phone"`
	Email     string         `json:"email"`
	Status    sql.NullString `json:"status"`
	CreatedAt time.Time      `json:"created_at"`
}

func (q *Queries) ListSuperAdmins(ctx context.Context, arg ListSuperAdminsParams) ([]ListSuperAdminsRow, error) {
	rows, err := q.db.QueryContext(ctx, listSuperAdmins, arg.Limit, arg.Offset, arg.UserType)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListSuperAdminsRow{}
	for rows.Next() {
		var i ListSuperAdminsRow
		if err := rows.Scan(
			&i.UserID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Phone,
			&i.Email,
			&i.Status,
			&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 listUsers = `-- name: ListUsers :many
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at FROM users
ORDER BY id LIMIT $1
OFFSET $2
`

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

func (q *Queries) ListUsers(ctx context.Context, arg ListUsersParams) ([]User, error) {
	rows, err := q.db.QueryContext(ctx, listUsers, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []User{}
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Stamp,
			&i.Signature,
			&i.Phone,
			&i.Status,
			&i.UserType,
			&i.Email,
			&i.Password,
			&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 nPPOInspectorsStats = `-- name: NPPOInspectorsStats :one
SELECT count(id) from users
WHERE user_type = 'NPPO-INSPECTOR'
`

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

const packhouseInspectorCount = `-- name: PackhouseInspectorCount :one
SELECT COUNT(id)
FROM users
WHERE user_type = 'PACKHOUSE-INSPECTOR'
`

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

const qualityControllerCount = `-- name: QualityControllerCount :one
SELECT COUNT(id)
FROM users
WHERE user_type = 'QUALITY-CONTROLLER'
`

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

const qualityControllerExporterCount = `-- name: QualityControllerExporterCount :one
SELECT COUNT(users.id)
FROM  users
LEFT JOIN user_exporter on user_exporter.user_id = users.id
WHERE users.user_type = 'QUALITY-CONTROLLER'
AND   user_exporter.exportcompany_id = $1
`

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

const returnAllBorderInspectors = `-- name: ReturnAllBorderInspectors :many
SELECT user_id FROM roastercount
ORDER BY counter ASC LIMIT 2
`

func (q *Queries) ReturnAllBorderInspectors(ctx context.Context) ([]int32, error) {
	rows, err := q.db.QueryContext(ctx, returnAllBorderInspectors)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []int32{}
	for rows.Next() {
		var user_id int32
		if err := rows.Scan(&user_id); err != nil {
			return nil, err
		}
		items = append(items, user_id)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const returnAllExporterUsers = `-- name: ReturnAllExporterUsers :many
SELECT users.fullname, users.phone, users.id FROM users
INNER JOIN user_exporter on user_exporter.user_id = users.id
WHERE  user_exporter.exportcompany_id = $1
AND    users.user_type IN ('QUALITY-CONTROLLER','EXPORTER','MANAGER')
`

type ReturnAllExporterUsersRow struct {
	Fullname string `json:"fullname"`
	Phone    string `json:"phone"`
	ID       int64  `json:"id"`
}

func (q *Queries) ReturnAllExporterUsers(ctx context.Context, exportcompanyID int32) ([]ReturnAllExporterUsersRow, error) {
	rows, err := q.db.QueryContext(ctx, returnAllExporterUsers, exportcompanyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnAllExporterUsersRow{}
	for rows.Next() {
		var i ReturnAllExporterUsersRow
		if err := rows.Scan(&i.Fullname, &i.Phone, &i.ID); 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 returnAllQualityControllers = `-- name: ReturnAllQualityControllers :many
 SELECT users.fullname,users.id as user_id,
 users.phone,users.user_type,users.email,
 users.created_at as created_at,users.status FROM users
 LEFT JOIN user_exporter on user_exporter.user_id = users.id
 WHERE user_type = $3 LIMIT $1
 OFFSET $2
`

type ReturnAllQualityControllersParams struct {
	Limit    int32  `json:"limit"`
	Offset   int32  `json:"offset"`
	UserType string `json:"user_type"`
}

type ReturnAllQualityControllersRow struct {
	Fullname  string         `json:"fullname"`
	UserID    int64          `json:"user_id"`
	Phone     string         `json:"phone"`
	UserType  string         `json:"user_type"`
	Email     string         `json:"email"`
	CreatedAt time.Time      `json:"created_at"`
	Status    sql.NullString `json:"status"`
}

func (q *Queries) ReturnAllQualityControllers(ctx context.Context, arg ReturnAllQualityControllersParams) ([]ReturnAllQualityControllersRow, error) {
	rows, err := q.db.QueryContext(ctx, returnAllQualityControllers, arg.Limit, arg.Offset, arg.UserType)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnAllQualityControllersRow{}
	for rows.Next() {
		var i ReturnAllQualityControllersRow
		if err := rows.Scan(
			&i.Fullname,
			&i.UserID,
			&i.Phone,
			&i.UserType,
			&i.Email,
			&i.CreatedAt,
			&i.Status,
		); 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 returnBorderInspectorForExitPointDate = `-- name: ReturnBorderInspectorForExitPointDate :many
SELECT users.fullname, users.phone, users.id, exitpoints.expdescription, exitpoints.expcode FROM users
INNER  JOIN exitpointroaster on exitpointroaster.user_id = users.id
INNER  JOIN plannedconsignments_tpdetails on plannedconsignments_tpdetails.exitpoint_id = exitpointroaster.exitpoint_id
INNER  JOIN exitpoints on exitpoints.id   =  exitpointroaster.exitpoint_id
WHERE  users.user_type = 'BORDER-INSPECTOR'
AND    exitpointroaster.date = $1
AND    plannedconsignments_tpdetails.plannedconsignments_id = $2
`

type ReturnBorderInspectorForExitPointDateParams struct {
	Date                  time.Time `json:"date"`
	PlannedconsignmentsID string    `json:"plannedconsignments_id"`
}

type ReturnBorderInspectorForExitPointDateRow struct {
	Fullname       string `json:"fullname"`
	Phone          string `json:"phone"`
	ID             int64  `json:"id"`
	Expdescription string `json:"expdescription"`
	Expcode        string `json:"expcode"`
}

func (q *Queries) ReturnBorderInspectorForExitPointDate(ctx context.Context, arg ReturnBorderInspectorForExitPointDateParams) ([]ReturnBorderInspectorForExitPointDateRow, error) {
	rows, err := q.db.QueryContext(ctx, returnBorderInspectorForExitPointDate, arg.Date, arg.PlannedconsignmentsID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnBorderInspectorForExitPointDateRow{}
	for rows.Next() {
		var i ReturnBorderInspectorForExitPointDateRow
		if err := rows.Scan(
			&i.Fullname,
			&i.Phone,
			&i.ID,
			&i.Expdescription,
			&i.Expcode,
		); 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 returnBorderInspectors = `-- name: ReturnBorderInspectors :many
 SELECT users.id as user_id,users.status,fullname,phone,user_type,email,users.created_at as created_at FROM users
 WHERE user_type = 'BORDER-INSPECTOR'
 LIMIT $1
 OFFSET $2
`

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

type ReturnBorderInspectorsRow struct {
	UserID    int64          `json:"user_id"`
	Status    sql.NullString `json:"status"`
	Fullname  string         `json:"fullname"`
	Phone     string         `json:"phone"`
	UserType  string         `json:"user_type"`
	Email     string         `json:"email"`
	CreatedAt time.Time      `json:"created_at"`
}

func (q *Queries) ReturnBorderInspectors(ctx context.Context, arg ReturnBorderInspectorsParams) ([]ReturnBorderInspectorsRow, error) {
	rows, err := q.db.QueryContext(ctx, returnBorderInspectors, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnBorderInspectorsRow{}
	for rows.Next() {
		var i ReturnBorderInspectorsRow
		if err := rows.Scan(
			&i.UserID,
			&i.Status,
			&i.Fullname,
			&i.Phone,
			&i.UserType,
			&i.Email,
			&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 returnExporterAdmin = `-- name: ReturnExporterAdmin :one
 SELECT users.id as user_id, users.status,users.regno, users.nin, users.fullname,users.phone,
 users.user_type,users.email,users.created_at as created_at, users.signature, users.stamp FROM users
 LEFT JOIN user_exporter on user_exporter.user_id = users.id
 WHERE user_type = 'EXPORTER' AND user_exporter.exportcompany_id = $1
 LIMIT 1
`

type ReturnExporterAdminRow struct {
	UserID    int64          `json:"user_id"`
	Status    sql.NullString `json:"status"`
	Regno     sql.NullString `json:"regno"`
	Nin       sql.NullString `json:"nin"`
	Fullname  string         `json:"fullname"`
	Phone     string         `json:"phone"`
	UserType  string         `json:"user_type"`
	Email     string         `json:"email"`
	CreatedAt time.Time      `json:"created_at"`
	Signature sql.NullString `json:"signature"`
	Stamp     sql.NullString `json:"stamp"`
}

func (q *Queries) ReturnExporterAdmin(ctx context.Context, exportcompanyID int32) (ReturnExporterAdminRow, error) {
	row := q.db.QueryRowContext(ctx, returnExporterAdmin, exportcompanyID)
	var i ReturnExporterAdminRow
	err := row.Scan(
		&i.UserID,
		&i.Status,
		&i.Regno,
		&i.Nin,
		&i.Fullname,
		&i.Phone,
		&i.UserType,
		&i.Email,
		&i.CreatedAt,
		&i.Signature,
		&i.Stamp,
	)
	return i, err
}

const returnExporterAdmins = `-- name: ReturnExporterAdmins :many
 SELECT users.id as user_id, exportcompanies.id as exportcompany_id, users.status,users.regno, users.nin, fullname,phone,user_type,email,users.created_at as created_at FROM user_exporter
 LEFT JOIN exportcompanies on exportcompanies.id = user_exporter.exportcompany_id
 LEFT JOIN users on users.id = user_exporter.user_id
 WHERE exportcompanies.id = $3
 AND   users.user_type IN ('EXPORTER','MANAGER')
 ORDER BY users.created_at DESC
 LIMIT  $1
 OFFSET $2
`

type ReturnExporterAdminsParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
	ID     int64 `json:"id"`
}

type ReturnExporterAdminsRow struct {
	UserID          sql.NullInt64  `json:"user_id"`
	ExportcompanyID sql.NullInt64  `json:"exportcompany_id"`
	Status          sql.NullString `json:"status"`
	Regno           sql.NullString `json:"regno"`
	Nin             sql.NullString `json:"nin"`
	Fullname        sql.NullString `json:"fullname"`
	Phone           sql.NullString `json:"phone"`
	UserType        sql.NullString `json:"user_type"`
	Email           sql.NullString `json:"email"`
	CreatedAt       sql.NullTime   `json:"created_at"`
}

func (q *Queries) ReturnExporterAdmins(ctx context.Context, arg ReturnExporterAdminsParams) ([]ReturnExporterAdminsRow, error) {
	rows, err := q.db.QueryContext(ctx, returnExporterAdmins, arg.Limit, arg.Offset, arg.ID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnExporterAdminsRow{}
	for rows.Next() {
		var i ReturnExporterAdminsRow
		if err := rows.Scan(
			&i.UserID,
			&i.ExportcompanyID,
			&i.Status,
			&i.Regno,
			&i.Nin,
			&i.Fullname,
			&i.Phone,
			&i.UserType,
			&i.Email,
			&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 returnNPPOInspectors = `-- name: ReturnNPPOInspectors :many
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at from users
WHERE user_type = 'NPPO-INSPECTOR'
`

func (q *Queries) ReturnNPPOInspectors(ctx context.Context) ([]User, error) {
	rows, err := q.db.QueryContext(ctx, returnNPPOInspectors)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []User{}
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Stamp,
			&i.Signature,
			&i.Phone,
			&i.Status,
			&i.UserType,
			&i.Email,
			&i.Password,
			&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 returnPackHouseInspectorForExporter = `-- name: ReturnPackHouseInspectorForExporter :one
SELECT users.fullname, users.phone, packhouses.phname, packhouses.contactphone, packhouses.streetaddress, exportcompanies.expname FROM users
INNER JOIN user_exporter on user_exporter.user_id = users.id
INNER JOIN packhouse_exporter on packhouse_exporter.exportcompany_id = user_exporter.exportcompany_id
INNER JOIN packhouses         on packhouses.id   = packhouse_exporter.packhouse_id
INNER JOIN exportcompanies    on exportcompanies.id = user_exporter.exportcompany_id
WHERE users.user_type = 'PACKHOUSE-INSPECTOR'
AND   user_exporter.exportcompany_id = $1
`

type ReturnPackHouseInspectorForExporterRow struct {
	Fullname      string `json:"fullname"`
	Phone         string `json:"phone"`
	Phname        string `json:"phname"`
	Contactphone  string `json:"contactphone"`
	Streetaddress string `json:"streetaddress"`
	Expname       string `json:"expname"`
}

func (q *Queries) ReturnPackHouseInspectorForExporter(ctx context.Context, exportcompanyID int32) (ReturnPackHouseInspectorForExporterRow, error) {
	row := q.db.QueryRowContext(ctx, returnPackHouseInspectorForExporter, exportcompanyID)
	var i ReturnPackHouseInspectorForExporterRow
	err := row.Scan(
		&i.Fullname,
		&i.Phone,
		&i.Phname,
		&i.Contactphone,
		&i.Streetaddress,
		&i.Expname,
	)
	return i, err
}

const returnPackhouseInspectors = `-- name: ReturnPackhouseInspectors :many
 SELECT users.id as user_id,users.status, fullname,phone,user_type,email,users.created_at as created_at FROM users
 WHERE user_type = 'PACKHOUSE-INSPECTOR' LIMIT $1
 OFFSET $2
`

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

type ReturnPackhouseInspectorsRow struct {
	UserID    int64          `json:"user_id"`
	Status    sql.NullString `json:"status"`
	Fullname  string         `json:"fullname"`
	Phone     string         `json:"phone"`
	UserType  string         `json:"user_type"`
	Email     string         `json:"email"`
	CreatedAt time.Time      `json:"created_at"`
}

func (q *Queries) ReturnPackhouseInspectors(ctx context.Context, arg ReturnPackhouseInspectorsParams) ([]ReturnPackhouseInspectorsRow, error) {
	rows, err := q.db.QueryContext(ctx, returnPackhouseInspectors, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnPackhouseInspectorsRow{}
	for rows.Next() {
		var i ReturnPackhouseInspectorsRow
		if err := rows.Scan(
			&i.UserID,
			&i.Status,
			&i.Fullname,
			&i.Phone,
			&i.UserType,
			&i.Email,
			&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 returnPaginatedLogs = `-- name: ReturnPaginatedLogs :many
SELECT userlogs.id,users.fullname,
userlogs.user_id,userlogs.activity_type,
userlogs.activity_description,userlogs.usertype,
userlogs.organisation,userlogs.organisationid,
userlogs.ip_address,userlogs.created_at,userlogs.status,
userlogs.user_agent from userlogs
JOIN users on users.id = userlogs.user_id
LIMIT $1 OFFSET $2
`

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

type ReturnPaginatedLogsRow struct {
	ID                  uuid.UUID      `json:"id"`
	Fullname            string         `json:"fullname"`
	UserID              int32          `json:"user_id"`
	ActivityType        string         `json:"activity_type"`
	ActivityDescription string         `json:"activity_description"`
	Usertype            sql.NullString `json:"usertype"`
	Organisation        sql.NullString `json:"organisation"`
	Organisationid      sql.NullInt32  `json:"organisationid"`
	IpAddress           string         `json:"ip_address"`
	CreatedAt           time.Time      `json:"created_at"`
	Status              sql.NullString `json:"status"`
	UserAgent           string         `json:"user_agent"`
}

func (q *Queries) ReturnPaginatedLogs(ctx context.Context, arg ReturnPaginatedLogsParams) ([]ReturnPaginatedLogsRow, error) {
	rows, err := q.db.QueryContext(ctx, returnPaginatedLogs, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnPaginatedLogsRow{}
	for rows.Next() {
		var i ReturnPaginatedLogsRow
		if err := rows.Scan(
			&i.ID,
			&i.Fullname,
			&i.UserID,
			&i.ActivityType,
			&i.ActivityDescription,
			&i.Usertype,
			&i.Organisation,
			&i.Organisationid,
			&i.IpAddress,
			&i.CreatedAt,
			&i.Status,
			&i.UserAgent,
		); 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 returnPaginatedNPPOInspectors = `-- name: ReturnPaginatedNPPOInspectors :many
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at from users
WHERE user_type = 'NPPO-INSPECTOR'
LIMIT $1 OFFSET $2
`

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

func (q *Queries) ReturnPaginatedNPPOInspectors(ctx context.Context, arg ReturnPaginatedNPPOInspectorsParams) ([]User, error) {
	rows, err := q.db.QueryContext(ctx, returnPaginatedNPPOInspectors, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []User{}
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Stamp,
			&i.Signature,
			&i.Phone,
			&i.Status,
			&i.UserType,
			&i.Email,
			&i.Password,
			&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 returnQcpsByEmail = `-- name: ReturnQcpsByEmail :one
SELECT  id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at from users
WHERE  user_type = 'QUALITY-CONTROLLER'
AND    email = $1 LIMIT 1
`

func (q *Queries) ReturnQcpsByEmail(ctx context.Context, email string) (User, error) {
	row := q.db.QueryRowContext(ctx, returnQcpsByEmail, email)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const returnQcpsPerExporter = `-- name: ReturnQcpsPerExporter :many
SELECT users.user_type as usertype,users.id as user_id, users.fullname as fullname from user_exporter
INNER JOIN users on users.id = user_exporter.user_id
WHERE user_exporter.exportcompany_id = $1
AND   users.user_type = 'QUALITY-CONTROLLER'
`

type ReturnQcpsPerExporterRow struct {
	Usertype string `json:"usertype"`
	UserID   int64  `json:"user_id"`
	Fullname string `json:"fullname"`
}

func (q *Queries) ReturnQcpsPerExporter(ctx context.Context, exportcompanyID int32) ([]ReturnQcpsPerExporterRow, error) {
	rows, err := q.db.QueryContext(ctx, returnQcpsPerExporter, exportcompanyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnQcpsPerExporterRow{}
	for rows.Next() {
		var i ReturnQcpsPerExporterRow
		if err := rows.Scan(&i.Usertype, &i.UserID, &i.Fullname); 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 returnQualityControllers = `-- name: ReturnQualityControllers :many
 SELECT fullname,users.id as user_id, users.nin,users.status,user_exporter.exportcompany_id,users.phone,users.user_type,users.email,users.created_at as created_at FROM users
 LEFT JOIN user_exporter on user_exporter.user_id = users.id
 WHERE user_type = $1 AND user_exporter.exportcompany_id = $2
`

type ReturnQualityControllersParams struct {
	UserType        string `json:"user_type"`
	ExportcompanyID int32  `json:"exportcompany_id"`
}

type ReturnQualityControllersRow struct {
	Fullname        string         `json:"fullname"`
	UserID          int64          `json:"user_id"`
	Nin             sql.NullString `json:"nin"`
	Status          sql.NullString `json:"status"`
	ExportcompanyID sql.NullInt32  `json:"exportcompany_id"`
	Phone           string         `json:"phone"`
	UserType        string         `json:"user_type"`
	Email           string         `json:"email"`
	CreatedAt       time.Time      `json:"created_at"`
}

func (q *Queries) ReturnQualityControllers(ctx context.Context, arg ReturnQualityControllersParams) ([]ReturnQualityControllersRow, error) {
	rows, err := q.db.QueryContext(ctx, returnQualityControllers, arg.UserType, arg.ExportcompanyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnQualityControllersRow{}
	for rows.Next() {
		var i ReturnQualityControllersRow
		if err := rows.Scan(
			&i.Fullname,
			&i.UserID,
			&i.Nin,
			&i.Status,
			&i.ExportcompanyID,
			&i.Phone,
			&i.UserType,
			&i.Email,
			&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 returnQualityControllersPerExporter = `-- name: ReturnQualityControllersPerExporter :many
SELECT users.fullname,users.nin,users.regno,users.phone,
users.user_type,users.email,users.id,user_exporter.exportcompany_id from users
JOIN  user_exporter on user_exporter.user_id = users.id
WHERE users.user_type = 'QUALITY-CONTROLLER'
AND      user_exporter.exportcompany_id = $1 
ORDER BY users.created_at DESC
`

type ReturnQualityControllersPerExporterRow struct {
	Fullname        string         `json:"fullname"`
	Nin             sql.NullString `json:"nin"`
	Regno           sql.NullString `json:"regno"`
	Phone           string         `json:"phone"`
	UserType        string         `json:"user_type"`
	Email           string         `json:"email"`
	ID              int64          `json:"id"`
	ExportcompanyID int32          `json:"exportcompany_id"`
}

func (q *Queries) ReturnQualityControllersPerExporter(ctx context.Context, exportcompanyID int32) ([]ReturnQualityControllersPerExporterRow, error) {
	rows, err := q.db.QueryContext(ctx, returnQualityControllersPerExporter, exportcompanyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnQualityControllersPerExporterRow{}
	for rows.Next() {
		var i ReturnQualityControllersPerExporterRow
		if err := rows.Scan(
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Phone,
			&i.UserType,
			&i.Email,
			&i.ID,
			&i.ExportcompanyID,
		); 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 returnSupportAccounts = `-- name: ReturnSupportAccounts :many
 SELECT users.id as user_id, users.status, fullname,phone,user_type,email,users.created_at as created_at FROM users
 LEFT JOIN user_exporter on user_exporter.user_id = users.id
 WHERE user_type = 'SUPPORT'
 LIMIT $1
 OFFSET $2
`

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

type ReturnSupportAccountsRow struct {
	UserID    int64          `json:"user_id"`
	Status    sql.NullString `json:"status"`
	Fullname  string         `json:"fullname"`
	Phone     string         `json:"phone"`
	UserType  string         `json:"user_type"`
	Email     string         `json:"email"`
	CreatedAt time.Time      `json:"created_at"`
}

func (q *Queries) ReturnSupportAccounts(ctx context.Context, arg ReturnSupportAccountsParams) ([]ReturnSupportAccountsRow, error) {
	rows, err := q.db.QueryContext(ctx, returnSupportAccounts, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnSupportAccountsRow{}
	for rows.Next() {
		var i ReturnSupportAccountsRow
		if err := rows.Scan(
			&i.UserID,
			&i.Status,
			&i.Fullname,
			&i.Phone,
			&i.UserType,
			&i.Email,
			&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 returnUserCompanies = `-- name: ReturnUserCompanies :many
 SELECT expname,expregno,exprexno,exportcompanies.id exporter_id from user_exporter
 JOIN   users  on users.id = user_exporter.user_id
 JOIN   exportcompanies on exportcompanies.id = user_exporter.exportcompany_id
 WHERE  user_exporter.user_id = $1
`

type ReturnUserCompaniesRow struct {
	Expname    string         `json:"expname"`
	Expregno   string         `json:"expregno"`
	Exprexno   sql.NullString `json:"exprexno"`
	ExporterID int64          `json:"exporter_id"`
}

func (q *Queries) ReturnUserCompanies(ctx context.Context, userID int32) ([]ReturnUserCompaniesRow, error) {
	rows, err := q.db.QueryContext(ctx, returnUserCompanies, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnUserCompaniesRow{}
	for rows.Next() {
		var i ReturnUserCompaniesRow
		if err := rows.Scan(
			&i.Expname,
			&i.Expregno,
			&i.Exprexno,
			&i.ExporterID,
		); 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 returnUserExportCompanies = `-- name: ReturnUserExportCompanies :many
 SELECT expname,expregno,exprexno,exportcompanies.id exporter_id from user_exporter
 JOIN   users on users.id = user_exporter.user_id
 JOIN   exportcompanies on exportcompanies.id = user_exporter.exportcompany_id
 WHERE  user_exporter.exportcompany_id = $1
`

type ReturnUserExportCompaniesRow struct {
	Expname    string         `json:"expname"`
	Expregno   string         `json:"expregno"`
	Exprexno   sql.NullString `json:"exprexno"`
	ExporterID int64          `json:"exporter_id"`
}

func (q *Queries) ReturnUserExportCompanies(ctx context.Context, exportcompanyID int32) ([]ReturnUserExportCompaniesRow, error) {
	rows, err := q.db.QueryContext(ctx, returnUserExportCompanies, exportcompanyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnUserExportCompaniesRow{}
	for rows.Next() {
		var i ReturnUserExportCompaniesRow
		if err := rows.Scan(
			&i.Expname,
			&i.Expregno,
			&i.Exprexno,
			&i.ExporterID,
		); 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 returnUserPackhouses = `-- name: ReturnUserPackhouses :many
 SELECT  phname,phregno,regstatus,users.created_at from user_packhouse
 JOIN    users on users.id = user_packhouse.user_id
 JOIN    packhouses on packhouses.id = user_packhouse.packhouse_id
 WHERE   user_packhouse.user_id = $1
`

type ReturnUserPackhousesRow struct {
	Phname    string    `json:"phname"`
	Phregno   string    `json:"phregno"`
	Regstatus string    `json:"regstatus"`
	CreatedAt time.Time `json:"created_at"`
}

func (q *Queries) ReturnUserPackhouses(ctx context.Context, userID int32) ([]ReturnUserPackhousesRow, error) {
	rows, err := q.db.QueryContext(ctx, returnUserPackhouses, userID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnUserPackhousesRow{}
	for rows.Next() {
		var i ReturnUserPackhousesRow
		if err := rows.Scan(
			&i.Phname,
			&i.Phregno,
			&i.Regstatus,
			&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 returnZonalBorderInspectors = `-- name: ReturnZonalBorderInspectors :many
 SELECT users.id as user_id,users.status, fullname,phone,user_type,email,users.created_at as created_at FROM users
 WHERE user_type = 'ZONAL-BORDER-INSPECTOR' LIMIT $1
 OFFSET $2
`

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

type ReturnZonalBorderInspectorsRow struct {
	UserID    int64          `json:"user_id"`
	Status    sql.NullString `json:"status"`
	Fullname  string         `json:"fullname"`
	Phone     string         `json:"phone"`
	UserType  string         `json:"user_type"`
	Email     string         `json:"email"`
	CreatedAt time.Time      `json:"created_at"`
}

func (q *Queries) ReturnZonalBorderInspectors(ctx context.Context, arg ReturnZonalBorderInspectorsParams) ([]ReturnZonalBorderInspectorsRow, error) {
	rows, err := q.db.QueryContext(ctx, returnZonalBorderInspectors, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnZonalBorderInspectorsRow{}
	for rows.Next() {
		var i ReturnZonalBorderInspectorsRow
		if err := rows.Scan(
			&i.UserID,
			&i.Status,
			&i.Fullname,
			&i.Phone,
			&i.UserType,
			&i.Email,
			&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 returnZonalInspectors = `-- name: ReturnZonalInspectors :many
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at from users
WHERE user_type = 'ZONAL-INSPECTOR'
`

func (q *Queries) ReturnZonalInspectors(ctx context.Context) ([]User, error) {
	rows, err := q.db.QueryContext(ctx, returnZonalInspectors)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []User{}
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Stamp,
			&i.Signature,
			&i.Phone,
			&i.Status,
			&i.UserType,
			&i.Email,
			&i.Password,
			&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 returnZonalPackhouseInspectors = `-- name: ReturnZonalPackhouseInspectors :many
 SELECT users.id as user_id,users.status, fullname,phone,user_type,email,users.created_at as created_at FROM users
 WHERE user_type = 'ZONAL-PACKHOUSE-INSPECTOR' LIMIT $1
 OFFSET $2
`

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

type ReturnZonalPackhouseInspectorsRow struct {
	UserID    int64          `json:"user_id"`
	Status    sql.NullString `json:"status"`
	Fullname  string         `json:"fullname"`
	Phone     string         `json:"phone"`
	UserType  string         `json:"user_type"`
	Email     string         `json:"email"`
	CreatedAt time.Time      `json:"created_at"`
}

func (q *Queries) ReturnZonalPackhouseInspectors(ctx context.Context, arg ReturnZonalPackhouseInspectorsParams) ([]ReturnZonalPackhouseInspectorsRow, error) {
	rows, err := q.db.QueryContext(ctx, returnZonalPackhouseInspectors, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ReturnZonalPackhouseInspectorsRow{}
	for rows.Next() {
		var i ReturnZonalPackhouseInspectorsRow
		if err := rows.Scan(
			&i.UserID,
			&i.Status,
			&i.Fullname,
			&i.Phone,
			&i.UserType,
			&i.Email,
			&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 searchBorderInspectors = `-- name: SearchBorderInspectors :many
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at
FROM  users
WHERE user_type = 'BORDER-INSPECTOR'
AND   LOWER(fullname) LIKE '%' || LOWER($1) || '%'
`

func (q *Queries) SearchBorderInspectors(ctx context.Context, lower string) ([]User, error) {
	rows, err := q.db.QueryContext(ctx, searchBorderInspectors, lower)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []User{}
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Stamp,
			&i.Signature,
			&i.Phone,
			&i.Status,
			&i.UserType,
			&i.Email,
			&i.Password,
			&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 searchPackhouseInspector = `-- name: SearchPackhouseInspector :many
SELECT users.id as user_id,users.status,users.fullname,
users.phone,users.user_type,users.email,users.created_at
FROM  users
WHERE user_type = 'PACKHOUSE-INSPECTOR'
AND   LOWER(fullname) LIKE '%' || LOWER($1) || '%'
`

type SearchPackhouseInspectorRow struct {
	UserID    int64          `json:"user_id"`
	Status    sql.NullString `json:"status"`
	Fullname  string         `json:"fullname"`
	Phone     string         `json:"phone"`
	UserType  string         `json:"user_type"`
	Email     string         `json:"email"`
	CreatedAt time.Time      `json:"created_at"`
}

func (q *Queries) SearchPackhouseInspector(ctx context.Context, lower string) ([]SearchPackhouseInspectorRow, error) {
	rows, err := q.db.QueryContext(ctx, searchPackhouseInspector, lower)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []SearchPackhouseInspectorRow{}
	for rows.Next() {
		var i SearchPackhouseInspectorRow
		if err := rows.Scan(
			&i.UserID,
			&i.Status,
			&i.Fullname,
			&i.Phone,
			&i.UserType,
			&i.Email,
			&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 searchQualityController = `-- name: SearchQualityController :many
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at
FROM  users
WHERE user_type = 'QUALITY-CONTROLLER'
AND   LOWER(fullname) LIKE '%' || LOWER($1) || '%'
`

func (q *Queries) SearchQualityController(ctx context.Context, lower string) ([]User, error) {
	rows, err := q.db.QueryContext(ctx, searchQualityController, lower)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []User{}
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Stamp,
			&i.Signature,
			&i.Phone,
			&i.Status,
			&i.UserType,
			&i.Email,
			&i.Password,
			&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 searchZonalBorderInspector = `-- name: SearchZonalBorderInspector :many
SELECT users.id as user_id,users.status,users.fullname,
users.phone,users.user_type,users.email,users.created_at
FROM  users
WHERE user_type = 'ZONAL-BORDER-INSPECTOR'
AND   LOWER(fullname) LIKE '%' || LOWER($1) || '%'
`

type SearchZonalBorderInspectorRow struct {
	UserID    int64          `json:"user_id"`
	Status    sql.NullString `json:"status"`
	Fullname  string         `json:"fullname"`
	Phone     string         `json:"phone"`
	UserType  string         `json:"user_type"`
	Email     string         `json:"email"`
	CreatedAt time.Time      `json:"created_at"`
}

func (q *Queries) SearchZonalBorderInspector(ctx context.Context, lower string) ([]SearchZonalBorderInspectorRow, error) {
	rows, err := q.db.QueryContext(ctx, searchZonalBorderInspector, lower)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []SearchZonalBorderInspectorRow{}
	for rows.Next() {
		var i SearchZonalBorderInspectorRow
		if err := rows.Scan(
			&i.UserID,
			&i.Status,
			&i.Fullname,
			&i.Phone,
			&i.UserType,
			&i.Email,
			&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 searchZonalInspectors = `-- name: SearchZonalInspectors :many
SELECT id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at
FROM  users
WHERE user_type = 'ZONAL-INSPECTOR'
AND   LOWER(fullname) LIKE '%' || LOWER($1) || '%'
`

func (q *Queries) SearchZonalInspectors(ctx context.Context, lower string) ([]User, error) {
	rows, err := q.db.QueryContext(ctx, searchZonalInspectors, lower)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []User{}
	for rows.Next() {
		var i User
		if err := rows.Scan(
			&i.ID,
			&i.Fullname,
			&i.Nin,
			&i.Regno,
			&i.Stamp,
			&i.Signature,
			&i.Phone,
			&i.Status,
			&i.UserType,
			&i.Email,
			&i.Password,
			&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 searchZonalPackhouseInspector = `-- name: SearchZonalPackhouseInspector :many
SELECT users.id as user_id,users.status,users.fullname,
users.phone,users.user_type,users.email,users.created_at
FROM  users
WHERE user_type = 'ZONAL-PACKHOUSE-INSPECTOR'
AND   LOWER(fullname) LIKE '%' || LOWER($1) || '%'
`

type SearchZonalPackhouseInspectorRow struct {
	UserID    int64          `json:"user_id"`
	Status    sql.NullString `json:"status"`
	Fullname  string         `json:"fullname"`
	Phone     string         `json:"phone"`
	UserType  string         `json:"user_type"`
	Email     string         `json:"email"`
	CreatedAt time.Time      `json:"created_at"`
}

func (q *Queries) SearchZonalPackhouseInspector(ctx context.Context, lower string) ([]SearchZonalPackhouseInspectorRow, error) {
	rows, err := q.db.QueryContext(ctx, searchZonalPackhouseInspector, lower)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []SearchZonalPackhouseInspectorRow{}
	for rows.Next() {
		var i SearchZonalPackhouseInspectorRow
		if err := rows.Scan(
			&i.UserID,
			&i.Status,
			&i.Fullname,
			&i.Phone,
			&i.UserType,
			&i.Email,
			&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 superAdminCount = `-- name: SuperAdminCount :one
SELECT COUNT(id)
FROM users
WHERE user_type = 'SUPER-ADMIN'
`

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

const supportAccountCount = `-- name: SupportAccountCount :one
SELECT COUNT(id)
FROM users
WHERE user_type = 'SUPPORT'
`

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

const switchInspectorRole = `-- name: SwitchInspectorRole :one
UPDATE users
  set
  user_type  = $2
WHERE id = $1
RETURNING id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at
`

type SwitchInspectorRoleParams struct {
	ID       int64  `json:"id"`
	UserType string `json:"user_type"`
}

func (q *Queries) SwitchInspectorRole(ctx context.Context, arg SwitchInspectorRoleParams) (User, error) {
	row := q.db.QueryRowContext(ctx, switchInspectorRole, arg.ID, arg.UserType)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const toggleUserActiveStatus = `-- name: ToggleUserActiveStatus :one
UPDATE users
  set
  status  = $2
WHERE id  = $1
AND   user_type = $3
RETURNING id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at
`

type ToggleUserActiveStatusParams struct {
	ID       int64          `json:"id"`
	Status   sql.NullString `json:"status"`
	UserType string         `json:"user_type"`
}

func (q *Queries) ToggleUserActiveStatus(ctx context.Context, arg ToggleUserActiveStatusParams) (User, error) {
	row := q.db.QueryRowContext(ctx, toggleUserActiveStatus, arg.ID, arg.Status, arg.UserType)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const totalUsersToday = `-- name: TotalUsersToday :one
SELECT COUNT(DISTINCT user_id) AS unique_users
FROM userlogs
WHERE
  created_at >= CURRENT_DATE
`

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

const updateExporterStamp = `-- name: UpdateExporterStamp :one
UPDATE users
  set
  stamp  = $2
WHERE id = $1
RETURNING id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at
`

type UpdateExporterStampParams struct {
	ID    int64          `json:"id"`
	Stamp sql.NullString `json:"stamp"`
}

func (q *Queries) UpdateExporterStamp(ctx context.Context, arg UpdateExporterStampParams) (User, error) {
	row := q.db.QueryRowContext(ctx, updateExporterStamp, arg.ID, arg.Stamp)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const updateUser = `-- name: UpdateUser :one
UPDATE users
  set
  fullname       = COALESCE($1, fullname),
  nin            = COALESCE($2, nin),
  regno          = COALESCE($3, regno),
  phone          = COALESCE($4, phone),
  stamp          = COALESCE($5, stamp),
  signature      = COALESCE($6, signature),
  email          = COALESCE($7, email),
  password       = COALESCE($8, password),
  user_type       = $9
WHERE id = $10
RETURNING id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at
`

type UpdateUserParams struct {
	Fullname  sql.NullString `json:"fullname"`
	Nin       sql.NullString `json:"nin"`
	Regno     sql.NullString `json:"regno"`
	Phone     sql.NullString `json:"phone"`
	Stamp     sql.NullString `json:"stamp"`
	Signature sql.NullString `json:"signature"`
	Email     sql.NullString `json:"email"`
	Password  sql.NullString `json:"password"`
	UserType  string         `json:"user_type"`
	ID        int64          `json:"id"`
}

func (q *Queries) UpdateUser(ctx context.Context, arg UpdateUserParams) (User, error) {
	row := q.db.QueryRowContext(ctx, updateUser,
		arg.Fullname,
		arg.Nin,
		arg.Regno,
		arg.Phone,
		arg.Stamp,
		arg.Signature,
		arg.Email,
		arg.Password,
		arg.UserType,
		arg.ID,
	)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const updateUserEmail = `-- name: UpdateUserEmail :one
UPDATE users
  set
  email  = $2
WHERE id = $1
RETURNING id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at
`

type UpdateUserEmailParams struct {
	ID    int64  `json:"id"`
	Email string `json:"email"`
}

func (q *Queries) UpdateUserEmail(ctx context.Context, arg UpdateUserEmailParams) (User, error) {
	row := q.db.QueryRowContext(ctx, updateUserEmail, arg.ID, arg.Email)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const updateUserPassword = `-- name: UpdateUserPassword :one
UPDATE users
   set
   password  = $1
WHERE id = $2
RETURNING id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at
`

type UpdateUserPasswordParams struct {
	Password string `json:"password"`
	ID       int64  `json:"id"`
}

func (q *Queries) UpdateUserPassword(ctx context.Context, arg UpdateUserPasswordParams) (User, error) {
	row := q.db.QueryRowContext(ctx, updateUserPassword, arg.Password, arg.ID)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const updateUserPhoneAndFullname = `-- name: UpdateUserPhoneAndFullname :one
UPDATE users
   set
   phone  = $1,
   fullname = $2
WHERE id = $3
RETURNING id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at
`

type UpdateUserPhoneAndFullnameParams struct {
	Phone    string `json:"phone"`
	Fullname string `json:"fullname"`
	ID       int64  `json:"id"`
}

func (q *Queries) UpdateUserPhoneAndFullname(ctx context.Context, arg UpdateUserPhoneAndFullnameParams) (User, error) {
	row := q.db.QueryRowContext(ctx, updateUserPhoneAndFullname, arg.Phone, arg.Fullname, arg.ID)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const updateUserSignature = `-- name: UpdateUserSignature :one
UPDATE users
  set
  signature  = $2
WHERE id = $1
RETURNING id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at
`

type UpdateUserSignatureParams struct {
	ID        int64          `json:"id"`
	Signature sql.NullString `json:"signature"`
}

func (q *Queries) UpdateUserSignature(ctx context.Context, arg UpdateUserSignatureParams) (User, error) {
	row := q.db.QueryRowContext(ctx, updateUserSignature, arg.ID, arg.Signature)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const updateUserStatus = `-- name: UpdateUserStatus :one
UPDATE users
   set
   status  = $2
WHERE id = $1
RETURNING id, fullname, nin, regno, stamp, signature, phone, status, user_type, email, password, created_at
`

type UpdateUserStatusParams struct {
	ID     int64          `json:"id"`
	Status sql.NullString `json:"status"`
}

func (q *Queries) UpdateUserStatus(ctx context.Context, arg UpdateUserStatusParams) (User, error) {
	row := q.db.QueryRowContext(ctx, updateUserStatus, arg.ID, arg.Status)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Fullname,
		&i.Nin,
		&i.Regno,
		&i.Stamp,
		&i.Signature,
		&i.Phone,
		&i.Status,
		&i.UserType,
		&i.Email,
		&i.Password,
		&i.CreatedAt,
	)
	return i, err
}

const userSignature = `-- name: UserSignature :one
SELECT  signature FROM users
WHERE  id = $1
`

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

const zonalBorderInspectorsStats = `-- name: ZonalBorderInspectorsStats :one
SELECT count(id) from users
WHERE user_type = 'ZONAL-BORDER-INSPECTOR'
`

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

const zonalInspectorsStats = `-- name: ZonalInspectorsStats :one
SELECT count(id) from users
WHERE user_type = 'ZONAL-INSPECTOR'
`

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

const zonalPackhouseInspectorsStats = `-- name: ZonalPackhouseInspectorsStats :one
SELECT count(id) from users
WHERE user_type = 'ZONAL-PACKHOUSE-INSPECTOR'
`

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