package comms

import (
	"context"

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

func TextJobComplete(ctx context.Context, txn db.Ex, id int64) error {
	statement := table.TextJob.UPDATE(
		table.TextJob.Completed,
	).SET(
		table.TextJob.Completed.SET(postgres.LOCALTIMESTAMP()),
	).WHERE(
		table.TextJob.ID.EQ(postgres.Int(id)),
	)
	return db.ExecuteNoneTx(ctx, txn, statement)
}
func TextJobFromID(ctx context.Context, txn db.Ex, id int64) (model.TextJob, error) {
	statement := table.TextJob.SELECT(
		table.TextJob.AllColumns,
	).FROM(table.TextJob).
		WHERE(table.TextJob.ID.EQ(postgres.Int(id)))
	return db.ExecuteOneTx[model.TextJob](ctx, txn, statement)
}
func TextJobInsert(ctx context.Context, txn db.Ex, m model.TextJob) (model.TextJob, error) {
	statement := table.TextJob.INSERT(
		table.TextJob.MutableColumns,
	).MODEL(m)
	return db.ExecuteOneTx[model.TextJob](ctx, txn, statement)
}
func TextJobsWaitingFromDestination(ctx context.Context, txn db.Ex, destination string) ([]model.TextJob, error) {
	statement := table.TextJob.SELECT(
		table.TextJob.AllColumns,
	).FROM(table.TextJob).
		WHERE(table.TextJob.Destination.EQ(postgres.String(destination)).AND(
			table.TextJob.Completed.IS_NULL()))
	return db.ExecuteManyTx[model.TextJob](ctx, txn, statement)
}
