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

package db

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

const createRedFlag = `-- name: CreateRedFlag :one
INSERT INTO redflags (
      crop_id,
      farmer_id,
      reason,
      user_id,
      stage,
      traceability_code,
      isactive,
      status
 ) VALUES (
    $1, $2, $3, $4, $5, $6, $7, $8) 
RETURNING id, crop_id, farmer_id, reason, user_id, stage, traceability_code, isactive, created_at, status
`

type CreateRedFlagParams struct {
	CropID           int32          `json:"crop_id"`
	FarmerID         int32          `json:"farmer_id"`
	Reason           string         `json:"reason"`
	UserID           int32          `json:"user_id"`
	Stage            string         `json:"stage"`
	TraceabilityCode string         `json:"traceability_code"`
	Isactive         bool           `json:"isactive"`
	Status           sql.NullString `json:"status"`
}

func (q *Queries) CreateRedFlag(ctx context.Context, arg CreateRedFlagParams) (Redflag, error) {
	row := q.db.QueryRowContext(ctx, createRedFlag,
		arg.CropID,
		arg.FarmerID,
		arg.Reason,
		arg.UserID,
		arg.Stage,
		arg.TraceabilityCode,
		arg.Isactive,
		arg.Status,
	)
	var i Redflag
	err := row.Scan(
		&i.ID,
		&i.CropID,
		&i.FarmerID,
		&i.Reason,
		&i.UserID,
		&i.Stage,
		&i.TraceabilityCode,
		&i.Isactive,
		&i.CreatedAt,
		&i.Status,
	)
	return i, err
}

const fetchRedFlags = `-- name: FetchRedFlags :many
SELECT redflags.id,redflags.reason,redflags.stage,redflags.created_at,redflags.traceability_code,redflags.status,
crops.cropname,farmers.fmname,users.fullname,redflags.isactive,redflags.status from redflags
JOIN     crops   on crops.id   = redflags.crop_id
JOIN     farmers on farmers.id = redflags.farmer_id
JOIN     users   on users.id   = redflags.user_id
Where    redflags.status = $3
ORDER BY redflags.created_at 
DESC LIMIT $1 OFFSET $2
`

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

type FetchRedFlagsRow struct {
	ID               int64          `json:"id"`
	Reason           string         `json:"reason"`
	Stage            string         `json:"stage"`
	CreatedAt        time.Time      `json:"created_at"`
	TraceabilityCode string         `json:"traceability_code"`
	Status           sql.NullString `json:"status"`
	Cropname         string         `json:"cropname"`
	Fmname           string         `json:"fmname"`
	Fullname         string         `json:"fullname"`
	Isactive         bool           `json:"isactive"`
	Status_2         sql.NullString `json:"status_2"`
}

func (q *Queries) FetchRedFlags(ctx context.Context, arg FetchRedFlagsParams) ([]FetchRedFlagsRow, error) {
	rows, err := q.db.QueryContext(ctx, fetchRedFlags, arg.Limit, arg.Offset, arg.Status)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []FetchRedFlagsRow{}
	for rows.Next() {
		var i FetchRedFlagsRow
		if err := rows.Scan(
			&i.ID,
			&i.Reason,
			&i.Stage,
			&i.CreatedAt,
			&i.TraceabilityCode,
			&i.Status,
			&i.Cropname,
			&i.Fmname,
			&i.Fullname,
			&i.Isactive,
			&i.Status_2,
		); 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 getApprovedRedflagCounts = `-- name: GetApprovedRedflagCounts :one
Select count(id) from redflags
Where status = 'RFapproved'
`

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

const getDeniedRedflagCounts = `-- name: GetDeniedRedflagCounts :one
Select count(id) from redflags
Where status = 'RFdenied'
`

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

const getPendingRedflagCounts = `-- name: GetPendingRedflagCounts :one
Select count(id) from redflags
Where status = 'RFpending'
`

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

const removeRedFlag = `-- name: RemoveRedFlag :one
UPDATE redflags
  set 
    status = $2
WHERE id = $1
RETURNING id, crop_id, farmer_id, reason, user_id, stage, traceability_code, isactive, created_at, status
`

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

func (q *Queries) RemoveRedFlag(ctx context.Context, arg RemoveRedFlagParams) (Redflag, error) {
	row := q.db.QueryRowContext(ctx, removeRedFlag, arg.ID, arg.Status)
	var i Redflag
	err := row.Scan(
		&i.ID,
		&i.CropID,
		&i.FarmerID,
		&i.Reason,
		&i.UserID,
		&i.Stage,
		&i.TraceabilityCode,
		&i.Isactive,
		&i.CreatedAt,
		&i.Status,
	)
	return i, err
}

const updateRedFlag = `-- name: UpdateRedFlag :one
UPDATE redflags
  set 
    reason = $3,
    status = $2
WHERE id = $1
RETURNING id, crop_id, farmer_id, reason, user_id, stage, traceability_code, isactive, created_at, status
`

type UpdateRedFlagParams struct {
	ID     int64          `json:"id"`
	Status sql.NullString `json:"status"`
	Reason string         `json:"reason"`
}

func (q *Queries) UpdateRedFlag(ctx context.Context, arg UpdateRedFlagParams) (Redflag, error) {
	row := q.db.QueryRowContext(ctx, updateRedFlag, arg.ID, arg.Status, arg.Reason)
	var i Redflag
	err := row.Scan(
		&i.ID,
		&i.CropID,
		&i.FarmerID,
		&i.Reason,
		&i.UserID,
		&i.Stage,
		&i.TraceabilityCode,
		&i.Isactive,
		&i.CreatedAt,
		&i.Status,
	)
	return i, err
}
