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

package db

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

const clientsCount = `-- name: ClientsCount :one
SELECT COUNT(id) from clients
Where status = $1 
AND   exportcompany_id = $2
`

type ClientsCountParams struct {
	Status          sql.NullString `json:"status"`
	ExportcompanyID int32          `json:"exportcompany_id"`
}

func (q *Queries) ClientsCount(ctx context.Context, arg ClientsCountParams) (int64, error) {
	row := q.db.QueryRowContext(ctx, clientsCount, arg.Status, arg.ExportcompanyID)
	var count int64
	err := row.Scan(&count)
	return count, err
}

const createClient = `-- name: CreateClient :one
INSERT INTO clients (           
     companyname,
     country_id,
     exportcompany_id,
     contactphone,
     contactemail,
     contactname
 ) VALUES (
    $1, $2, $3, $4, $5, $6
) 
RETURNING id, companyname, country_id, exportcompany_id, contactphone, contactname, contactemail, created_at, status
`

type CreateClientParams struct {
	Companyname     string `json:"companyname"`
	CountryID       int32  `json:"country_id"`
	ExportcompanyID int32  `json:"exportcompany_id"`
	Contactphone    string `json:"contactphone"`
	Contactemail    string `json:"contactemail"`
	Contactname     string `json:"contactname"`
}

func (q *Queries) CreateClient(ctx context.Context, arg CreateClientParams) (Client, error) {
	row := q.db.QueryRowContext(ctx, createClient,
		arg.Companyname,
		arg.CountryID,
		arg.ExportcompanyID,
		arg.Contactphone,
		arg.Contactemail,
		arg.Contactname,
	)
	var i Client
	err := row.Scan(
		&i.ID,
		&i.Companyname,
		&i.CountryID,
		&i.ExportcompanyID,
		&i.Contactphone,
		&i.Contactname,
		&i.Contactemail,
		&i.CreatedAt,
		&i.Status,
	)
	return i, err
}

const deactivateClient = `-- name: DeactivateClient :one
UPDATE clients
  set 
     status = $2
WHERE id = $1
RETURNING id, companyname, country_id, exportcompany_id, contactphone, contactname, contactemail, created_at, status
`

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

func (q *Queries) DeactivateClient(ctx context.Context, arg DeactivateClientParams) (Client, error) {
	row := q.db.QueryRowContext(ctx, deactivateClient, arg.ID, arg.Status)
	var i Client
	err := row.Scan(
		&i.ID,
		&i.Companyname,
		&i.CountryID,
		&i.ExportcompanyID,
		&i.Contactphone,
		&i.Contactname,
		&i.Contactemail,
		&i.CreatedAt,
		&i.Status,
	)
	return i, err
}

const deleteClient = `-- name: DeleteClient :exec
DELETE FROM clients WHERE id= $1
`

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

const getClient = `-- name: GetClient :one
SELECT id, companyname, country_id, exportcompany_id, contactphone, contactname, contactemail, created_at, status FROM clients
WHERE id = $1 LIMIT 1
`

func (q *Queries) GetClient(ctx context.Context, id int64) (Client, error) {
	row := q.db.QueryRowContext(ctx, getClient, id)
	var i Client
	err := row.Scan(
		&i.ID,
		&i.Companyname,
		&i.CountryID,
		&i.ExportcompanyID,
		&i.Contactphone,
		&i.Contactname,
		&i.Contactemail,
		&i.CreatedAt,
		&i.Status,
	)
	return i, err
}

const listClients = `-- name: ListClients :many
SELECT companyname,expname,country_id,countryname,exportcompany_id,clients.created_at created_at,
contactphone,contactemail,contactname,clients.id as client_id,clients.status client_status FROM clients
JOIN countries on countries.id = clients.country_id
JOIN exportcompanies on exportcompanies.id = clients.exportcompany_id
WHERE clients.exportcompany_id = $3 
AND clients.status = $4 
ORDER BY clients.id desc
LIMIT $1 OFFSET $2
`

type ListClientsParams struct {
	Limit           int32          `json:"limit"`
	Offset          int32          `json:"offset"`
	ExportcompanyID int32          `json:"exportcompany_id"`
	Status          sql.NullString `json:"status"`
}

type ListClientsRow struct {
	Companyname     string         `json:"companyname"`
	Expname         string         `json:"expname"`
	CountryID       int32          `json:"country_id"`
	Countryname     string         `json:"countryname"`
	ExportcompanyID int32          `json:"exportcompany_id"`
	CreatedAt       time.Time      `json:"created_at"`
	Contactphone    string         `json:"contactphone"`
	Contactemail    string         `json:"contactemail"`
	Contactname     string         `json:"contactname"`
	ClientID        int64          `json:"client_id"`
	ClientStatus    sql.NullString `json:"client_status"`
}

func (q *Queries) ListClients(ctx context.Context, arg ListClientsParams) ([]ListClientsRow, error) {
	rows, err := q.db.QueryContext(ctx, listClients,
		arg.Limit,
		arg.Offset,
		arg.ExportcompanyID,
		arg.Status,
	)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListClientsRow{}
	for rows.Next() {
		var i ListClientsRow
		if err := rows.Scan(
			&i.Companyname,
			&i.Expname,
			&i.CountryID,
			&i.Countryname,
			&i.ExportcompanyID,
			&i.CreatedAt,
			&i.Contactphone,
			&i.Contactemail,
			&i.Contactname,
			&i.ClientID,
			&i.ClientStatus,
		); 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 listExportCompanyClients = `-- name: ListExportCompanyClients :many
SELECT companyname,expname,country_id,
countryname,clients.exportcompany_id exporter_id,
clients.created_at created_at,contactphone,clients.id client_id,
contactemail,contactname,clients.status client_status FROM clients
JOIN countries on countries.id = clients.country_id
JOIN exportcompanies on exportcompanies.id = clients.exportcompany_id
WHERE clients.exportcompany_id = $1 ORDER BY clients.created_at DESC
`

type ListExportCompanyClientsRow struct {
	Companyname  string         `json:"companyname"`
	Expname      string         `json:"expname"`
	CountryID    int32          `json:"country_id"`
	Countryname  string         `json:"countryname"`
	ExporterID   int32          `json:"exporter_id"`
	CreatedAt    time.Time      `json:"created_at"`
	Contactphone string         `json:"contactphone"`
	ClientID     int64          `json:"client_id"`
	Contactemail string         `json:"contactemail"`
	Contactname  string         `json:"contactname"`
	ClientStatus sql.NullString `json:"client_status"`
}

func (q *Queries) ListExportCompanyClients(ctx context.Context, exportcompanyID int32) ([]ListExportCompanyClientsRow, error) {
	rows, err := q.db.QueryContext(ctx, listExportCompanyClients, exportcompanyID)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []ListExportCompanyClientsRow{}
	for rows.Next() {
		var i ListExportCompanyClientsRow
		if err := rows.Scan(
			&i.Companyname,
			&i.Expname,
			&i.CountryID,
			&i.Countryname,
			&i.ExporterID,
			&i.CreatedAt,
			&i.Contactphone,
			&i.ClientID,
			&i.Contactemail,
			&i.Contactname,
			&i.ClientStatus,
		); 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 updateClient = `-- name: UpdateClient :one
UPDATE clients
  set 
     companyname = COALESCE($1, companyname),
     country_id = COALESCE($2, country_id),
     exportcompany_id = COALESCE($3, exportcompany_id),
     contactphone = COALESCE($4, contactphone),
     contactemail = COALESCE($5, contactemail),
     contactname =  COALESCE($6, contactname)
WHERE id = $7
RETURNING id, companyname, country_id, exportcompany_id, contactphone, contactname, contactemail, created_at, status
`

type UpdateClientParams struct {
	Companyname     sql.NullString `json:"companyname"`
	CountryID       sql.NullInt32  `json:"country_id"`
	ExportcompanyID sql.NullInt32  `json:"exportcompany_id"`
	Contactphone    sql.NullString `json:"contactphone"`
	Contactemail    sql.NullString `json:"contactemail"`
	Contactname     sql.NullString `json:"contactname"`
	ID              int64          `json:"id"`
}

func (q *Queries) UpdateClient(ctx context.Context, arg UpdateClientParams) (Client, error) {
	row := q.db.QueryRowContext(ctx, updateClient,
		arg.Companyname,
		arg.CountryID,
		arg.ExportcompanyID,
		arg.Contactphone,
		arg.Contactemail,
		arg.Contactname,
		arg.ID,
	)
	var i Client
	err := row.Scan(
		&i.ID,
		&i.Companyname,
		&i.CountryID,
		&i.ExportcompanyID,
		&i.Contactphone,
		&i.Contactname,
		&i.Contactemail,
		&i.CreatedAt,
		&i.Status,
	)
	return i, err
}

const updateClientStatus = `-- name: UpdateClientStatus :one
UPDATE clients
  set 
     status = $2
WHERE id = $1
RETURNING id, companyname, country_id, exportcompany_id, contactphone, contactname, contactemail, created_at, status
`

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

func (q *Queries) UpdateClientStatus(ctx context.Context, arg UpdateClientStatusParams) (Client, error) {
	row := q.db.QueryRowContext(ctx, updateClientStatus, arg.ID, arg.Status)
	var i Client
	err := row.Scan(
		&i.ID,
		&i.Companyname,
		&i.CountryID,
		&i.ExportcompanyID,
		&i.Contactphone,
		&i.Contactname,
		&i.Contactemail,
		&i.CreatedAt,
		&i.Status,
	)
	return i, err
}
