package publicreport

import (
	"context"
	"errors"
	"fmt"
	//"time"

	//"github.com/Gleipnir-Technology/bob"
	"github.com/Gleipnir-Technology/jet/postgres"
	"source.gleipnir.technology/Gleipnir/nidus-sync/db"
	tablecomms "source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/comms/table"
	"source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/publicreport/model"
	"source.gleipnir.technology/Gleipnir/nidus-sync/db/gen/nidus-sync/publicreport/table"
)

type ReportUpdater = db.Updater[table.ReportTable, model.Report]

func NewReportUpdater() ReportUpdater {
	return db.NewUpdater[table.ReportTable, model.Report](
		table.Report,
		table.Report.ID,
	)
}

func ReportInsert(ctx context.Context, txn db.Ex, m model.Report) (model.Report, error) {
	statement := table.Report.INSERT(table.Report.MutableColumns).
		MODEL(m).
		RETURNING(table.Report.AllColumns)
	return db.ExecuteOneTx[model.Report](ctx, txn, statement)
}
func ReportsFromAddressID(ctx context.Context, txn db.Ex, org_id int64, address_id int64) ([]model.Report, error) {
	statement := table.Report.SELECT(
		table.Report.AllColumns,
	).FROM(table.Report).
		WHERE(table.Report.AddressID.EQ(postgres.Int(address_id)).AND(
			table.Report.OrganizationID.EQ(postgres.Int(org_id))))
	return db.ExecuteManyTx[model.Report](ctx, txn, statement)
}
func ReportFromID(ctx context.Context, txn db.Ex, report_id int64) (model.Report, error) {
	statement := table.Report.SELECT(
		table.Report.AllColumns,
	).FROM(table.Report).
		WHERE(table.Report.ID.EQ(postgres.Int(report_id)))
	return db.ExecuteOneTx[model.Report](ctx, txn, statement)
}
func ReportsFromIDs(ctx context.Context, report_ids []int64) ([]model.Report, error) {
	sql_ids := make([]postgres.Expression, len(report_ids))
	for i, report_id := range report_ids {
		sql_ids[i] = postgres.Int(report_id)
	}
	statement := table.Report.SELECT(
		table.Report.AllColumns,
	).FROM(table.Report).
		WHERE(table.Report.ID.IN(sql_ids...))
	return db.ExecuteMany[model.Report](ctx, statement)
}
func ReportsFromIDsForOrg(ctx context.Context, txn db.Ex, report_ids []int64, org_id int64) ([]model.Report, error) {
	sql_ids := make([]postgres.Expression, len(report_ids))
	for i, report_id := range report_ids {
		sql_ids[i] = postgres.Int(report_id)
	}
	statement := table.Report.SELECT(
		table.Report.AllColumns,
	).FROM(table.Report).
		WHERE(table.Report.ID.IN(sql_ids...).AND(
			table.Report.OrganizationID.EQ(postgres.Int(org_id))))
	return db.ExecuteManyTx[model.Report](ctx, txn, statement)
}
func ReportFromPublicID(ctx context.Context, txn db.Ex, public_id string) (*model.Report, error) {
	statement := table.Report.SELECT(
		table.Report.AllColumns,
	).FROM(table.Report).
		WHERE(table.Report.PublicID.EQ(postgres.String(public_id)))
	result, err := db.ExecuteOneTx[model.Report](ctx, txn, statement)
	if err != nil {
		if errors.Is(err, db.ErrNoRows) {
			return nil, nil
		}
		return nil, fmt.Errorf("query: %w", err)
	}
	return &result, nil
}
func ReportFromPublicIDForOrg(ctx context.Context, txn db.Ex, public_id string, org_id int64) (*model.Report, error) {
	statement := table.Report.SELECT(
		table.Report.AllColumns,
	).FROM(table.Report).
		WHERE(table.Report.PublicID.EQ(postgres.String(public_id)).AND(
			table.Report.OrganizationID.EQ(postgres.Int(org_id))))
	result, err := db.ExecuteOneTx[model.Report](ctx, txn, statement)
	if err != nil {
		if errors.Is(err, db.ErrNoRows) {
			return nil, nil
		}
		return nil, fmt.Errorf("query: %w", err)
	}
	return &result, nil
}
func ReportsFromReporterName(ctx context.Context, txn db.Ex, org_id int64, name string) ([]model.Report, error) {
	statement := table.Report.SELECT(
		table.Report.AllColumns,
	).FROM(table.Report).
		WHERE(table.Report.ReporterName.EQ(postgres.String(name)).AND(
			table.Report.OrganizationID.EQ(postgres.Int(org_id))))
	return db.ExecuteManyTx[model.Report](ctx, txn, statement)
}
func ReportsUnreviewedForOrganization(ctx context.Context, txn db.Ex, org_id int64) ([]model.Report, error) {
	statement := table.Report.SELECT(
		table.Report.AllColumns,
	).FROM(table.Report).
		WHERE(table.Report.Reviewed.IS_NULL().AND(
			table.Report.OrganizationID.EQ(postgres.Int(org_id))))
	return db.ExecuteManyTx[model.Report](ctx, txn, statement)
}
func ReportsFromReporterPhone(ctx context.Context, txn db.Ex, destination string) ([]model.Report, error) {
	statement := table.Report.SELECT(
		table.Report.AllColumns,
	).FROM(table.Report).
		FROM(table.Report.INNER_JOIN(
			tablecomms.TextJob,
			tablecomms.TextJob.ReportID.EQ(table.Report.ID),
		)).WHERE(
		tablecomms.TextJob.ReportID.IS_NOT_NULL().AND(
			tablecomms.TextJob.Destination.EQ(postgres.String(destination))).AND(
			table.Report.Status.EQ(postgres.NewEnumValue(model.Reportstatustype_Reported.String()))),
	)
	return db.ExecuteManyTx[model.Report](ctx, txn, statement)
}
func ReportUpdateReporterContactID(ctx context.Context, txn db.Ex, id int64, contact_id int64) error {
	statement := table.Report.UPDATE().
		SET(
			table.Report.ReporterContactID.SET(postgres.Int(contact_id)),
		).
		WHERE(table.Report.ID.EQ(postgres.Int(id)))
	return db.ExecuteNoneTx(ctx, txn, statement)
}
