package text

import (
	"context"
	"fmt"

	"source.gleipnir.technology/Gleipnir/nidus-sync/db"
	querycomms "source.gleipnir.technology/Gleipnir/nidus-sync/db/query/comms"
	"source.gleipnir.technology/Gleipnir/nidus-sync/platform/types"
	//"github.com/rs/zerolog/log"
)

func JobRespond(ctx context.Context, log_id int32) error {
	return respondText(ctx, log_id)
}
func JobSend(ctx context.Context, job_id int32) error {
	bxn := db.PGInstance.PGXPool
	job, err := querycomms.TextJobFromID(ctx, bxn, int64(job_id))
	if err != nil {
		return fmt.Errorf("find text: %w", err)
	}
	//log.Debug().Int32("job.id", job.ID).Msg("completing text job")
	return sendTextComplete(ctx, job)
}
func handleWaitingTextJobs(ctx context.Context, dst types.E164) error {
	bxn := db.PGInstance.PGXPool
	jobs, err := querycomms.TextJobsWaitingFromDestination(ctx, bxn, dst.PhoneString())
	if err != nil {
		return fmt.Errorf("query jobs: %w", err)
	}
	for _, job := range jobs {
		err = sendTextComplete(ctx, job)
		if err != nil {
			return fmt.Errorf("send text complete: %w", err)
		}
	}
	return nil
}
