package repository

import (
	"bpmsapi/entity"
	"log"
	"time"

	"github.com/jmoiron/sqlx"
)

type ReportActions interface {
	FetchOrderSummary(input entity.OrderSummaryInput) (*entity.OrderSummary, error)
	TechOrderSummary(input entity.TechOrderSummaryInput) (*entity.TechOrderSummary, error)
}

type ReportRepository struct {
	db *sqlx.DB
}

// auth-jwt
func ReportRepositoryInit(db *sqlx.DB) *ReportRepository {
	return &ReportRepository{
		db: db,
	}
}

func (reportRepo *ReportRepository) FetchOrderSummary(input entity.OrderSummaryInput) (*entity.OrderSummary, error) {
	dashboard := entity.OrderSummary{}
	if input.From_Date != "" && input.To_Date == "" {
		input.To_Date = time.Now().Format("2006-01-02")
	}
	var getSrqCompleted, getSrqPending, getCancelled error
	if input.From_Date != "" && input.To_Date != "" {
		getSrqCompleted = reportRepo.db.Get(&dashboard.Services_Completed, "SELECT COUNT(Srq_Id) FROM BPMS_SERVICE_REQUEST WHERE Srq_Stage_7 = 1 AND Srq_Stage_8 != 1 AND Technician_Id = ? AND Srq_Apartment_Id = ? AND date(Created_Date) BETWEEN ? AND ? ", input.Tech_Id, input.Srq_Apartment_Id, input.From_Date, input.To_Date)
		getSrqPending = reportRepo.db.Get(&dashboard.Services_Pending, "SELECT COUNT(Srq_Id) FROM BPMS_SERVICE_REQUEST WHERE Srq_Stage_7 != 1 AND Srq_Stage_8 != 1 AND Technician_Id = ? AND Srq_Apartment_Id = ? AND date(Created_Date) BETWEEN ? AND ? ", input.Tech_Id, input.Srq_Apartment_Id, input.From_Date, input.To_Date)
		getCancelled = reportRepo.db.Get(&dashboard.Cancelled, "SELECT COUNT(Srq_Id) FROM BPMS_SERVICE_REQUEST WHERE Srq_Stage_7 = 0 AND Srq_Stage_8 = 1 AND Technician_Id = ? AND Srq_Apartment_Id = ? AND date(Created_Date) BETWEEN ? AND ? ", input.Tech_Id, input.Srq_Apartment_Id, input.From_Date, input.To_Date)
	} else {
		getSrqCompleted = reportRepo.db.Get(&dashboard.Services_Completed, "SELECT COUNT(Srq_Id) FROM BPMS_SERVICE_REQUEST WHERE Srq_Stage_7 = 1 AND Srq_Stage_8 != 1 AND Technician_Id = ? AND Srq_Apartment_Id = ?", input.Tech_Id, input.Srq_Apartment_Id)
		getSrqPending = reportRepo.db.Get(&dashboard.Services_Pending, "SELECT COUNT(Srq_Id) FROM BPMS_SERVICE_REQUEST WHERE Srq_Stage_7 != 1 AND Srq_Stage_8 != 1 AND Technician_Id = ? AND Srq_Apartment_Id = ?", input.Tech_Id, input.Srq_Apartment_Id)
		getCancelled = reportRepo.db.Get(&dashboard.Cancelled, "SELECT COUNT(Srq_Id) FROM BPMS_SERVICE_REQUEST WHERE Srq_Stage_7 = 0 AND Srq_Stage_8 = 1 AND Technician_Id = ? AND Srq_Apartment_Id = ?", input.Tech_Id, input.Srq_Apartment_Id)
	}
	if getSrqCompleted != nil {
		log.Println("ReportRepository: FetchOrderSummary: servicecompletedcount: techid: ", input.Tech_Id, "apartmentid : ", input.Srq_Apartment_Id, " Error: ", getSrqCompleted.Error())
		return nil, getSrqCompleted
	}

	if getSrqPending != nil {
		log.Println("ReportRepository: FetchOrderSummary: servicependingcount: techid: ", input.Tech_Id, "apartmentid : ", input.Srq_Apartment_Id, " Error: ", getSrqPending.Error())
		return nil, getSrqPending
	}

	if getCancelled != nil {
		log.Println("ReportRepository: FetchOrderSummary: cancelledorderscount: techid: ", input.Tech_Id, "apartmentid : ", input.Srq_Apartment_Id, " Error: ", getCancelled.Error())
		return nil, getCancelled
	}

	return &dashboard, nil
}

func (reportRepo *ReportRepository) TechOrderSummary(input entity.TechOrderSummaryInput) (*entity.TechOrderSummary, error) {
	techDashboard := entity.TechOrderSummary{}

	var getSrqCompleted, getSrqPending, getSrqCancel, getSrqTotal error
	getSrqCompleted = reportRepo.db.Get(&techDashboard.Services_Completed, "SELECT COUNT(Srq_Id) FROM BPMS_SERVICE_REQUEST WHERE Srq_Stage_7 = 1 AND Srq_Stage_8 != 1 AND Technician_Id =?", input.Tech_Id)
	getSrqPending = reportRepo.db.Get(&techDashboard.Services_Pending, "SELECT COUNT(Srq_Id) FROM BPMS_SERVICE_REQUEST WHERE Srq_Stage_7 = 0 AND Srq_Stage_8 = 0 AND Technician_Id =?", input.Tech_Id)
	getSrqCancel = reportRepo.db.Get(&techDashboard.Cancelled, "SELECT COUNT(Srq_Id) FROM BPMS_SERVICE_REQUEST WHERE Srq_Stage_8 = 1 AND Technician_Id = ?", input.Tech_Id)
	getSrqTotal = reportRepo.db.Get(&techDashboard.Total_Srq, "SELECT COUNT(Srq_Id) FROM BPMS_SERVICE_REQUEST WHERE Technician_Id =?", input.Tech_Id)

	if getSrqCompleted != nil {
		log.Println("ReportRepository : TechOrderSummary : servicecompleted : techid :", input.Tech_Id)
		return nil, getSrqCompleted
	}
	if getSrqPending != nil {
		log.Println("ReportRepository : TechOrderSummary : servicepending : techid :", input.Tech_Id)
		return nil, getSrqPending
	}
	if getSrqCancel != nil {
		log.Println("ReportRepository : TechOrderSummary : servicecancel : techid :", input.Tech_Id)
		return nil, getSrqCancel
	}
	if getSrqTotal != nil {
		log.Println("ReportRepository : TechOrderSummary : totalservice : techid :", input.Tech_Id)
		return nil, getSrqTotal
	}

	return &techDashboard, nil

}
