package user

import (
	"errors"
	"strconv"

	"gorm.io/gorm"

	ed "simrs-vx/internal/domain/main-entities/doctor"
	ee "simrs-vx/internal/domain/main-entities/employee"
	el "simrs-vx/internal/domain/main-entities/laborant"
	emw "simrs-vx/internal/domain/main-entities/midwife"
	en "simrs-vx/internal/domain/main-entities/nurse"
	et "simrs-vx/internal/domain/main-entities/nutritionist"
	ep "simrs-vx/internal/domain/main-entities/pharmacist"
	esi "simrs-vx/internal/domain/main-entities/specialist-intern"
	e "simrs-vx/internal/domain/main-entities/user"

	ud "simrs-vx/internal/use-case/main-use-case/doctor"
	ue "simrs-vx/internal/use-case/main-use-case/employee"
	ul "simrs-vx/internal/use-case/main-use-case/laborant"
	umw "simrs-vx/internal/use-case/main-use-case/midwife"
	un "simrs-vx/internal/use-case/main-use-case/nurse"
	ut "simrs-vx/internal/use-case/main-use-case/nutritionist"
	upe "simrs-vx/internal/use-case/main-use-case/person"
	upa "simrs-vx/internal/use-case/main-use-case/person-address"
	upc "simrs-vx/internal/use-case/main-use-case/person-contact"
	up "simrs-vx/internal/use-case/main-use-case/pharmacist"
	usi "simrs-vx/internal/use-case/main-use-case/specialist-intern"

	erc "simrs-vx/internal/domain/references/common"
	ero "simrs-vx/internal/domain/references/organization"

	pl "simrs-vx/pkg/logger"
	pu "simrs-vx/pkg/use-case-helper"

	dg "github.com/karincake/apem/db-gorm-pg"
	d "github.com/karincake/dodol"
)

const source = "user"

func Create(input e.CreateDto) (*d.Data, error) {
	data := e.User{}

	event := pl.Event{
		Feature: "Create",
		Source:  source,
	}

	// Start log
	pl.SetLogInfo(&event, input.Sanitize(), "started", "create")

	err := dg.I.Transaction(func(tx *gorm.DB) error {
		mwRunner := newMiddlewareRunner(&event, tx)
		mwRunner.setMwType(pu.MWTPre)
		// Run pre-middleware
		if err := mwRunner.RunCreateMiddleware(createPreMw, &input, &data); err != nil {
			return err
		}

		if person_id, err := upe.CreateOrUpdatePerson(input.Person, &event, tx); err != nil {
			return err
		} else {
			input.Person_Id = person_id
		}

		for idx := range input.PersonAddresses {
			input.PersonAddresses[idx].Person_Id = *input.Person_Id
		}
		if err := upa.CreateOrUpdateBatch(input.PersonAddresses, &event, tx); err != nil {
			return err
		}

		for idx := range input.PersonContacts {
			input.PersonContacts[idx].Person_Id = *input.Person_Id
		}
		if err := upc.CreateOrUpdateBatch(input.PersonContacts, &event, tx); err != nil {
			return err
		}

		if resData, err := CreateData(input, &event, tx); err != nil {
			return err
		} else {
			data = *resData
		}

		if input.ContractPosition_Code == ero.CSCInt {
			createInt := esi.CreateDto{
				Person_Id:          input.Person_Id,
				Specialist_Code:    input.Specialist_Code,
				Subspecialist_Code: input.Subspecialist_Code,
				User_Id:            &data.Id,
			}
			if _, err := usi.CreateData(createInt, &event, tx); err != nil {
				return err
			}
			return nil
		}

		input.Employee.User_Id = &data.Id
		input.Employee.Person_Id = input.Person_Id
		employeeData, err := ue.CreateOrUpdate(setDataEmployeeUpdate(*input.Employee), &event, tx)
		if err != nil {
			return err
		}

		switch input.Employee.Position_Code {
		case ero.EPCDoc:
			createDoc := ed.CreateDto{
				Code:               input.Code,
				Employee_Id:        &employeeData.Id,
				IHS_Number:         input.IHS_Number,
				SIP_Number:         input.SIP_Number,
				Unit_Code:          input.Unit_Code,
				Specialist_Code:    input.Specialist_Code,
				Subspecialist_Code: input.Subspecialist_Code,
			}
			if _, err := ud.CreateData(createDoc, &event, tx); err != nil {
				return err
			}
		case ero.EPCNur:
			createNurse := en.CreateDto{
				Code:        input.Code,
				Employee_Id: &employeeData.Id,
				IHS_Number:  input.IHS_Number,
				Unit_Code:   input.Unit_Code,
				Infra_Code:  input.Infra_Code,
			}
			if _, err := un.CreateData(createNurse, &event, tx); err != nil {
				return err
			}
		case ero.EPCNut:
			createNutritionist := et.CreateDto{
				Code:        input.Code,
				Employee_Id: &employeeData.Id,
				IHS_Number:  input.IHS_Number,
			}
			if _, err := ut.CreateData(createNutritionist, &event, tx); err != nil {
				return err
			}
		case ero.EPCPha:
			createPharmacist := ep.CreateDto{
				Code:        input.Code,
				Employee_Id: &employeeData.Id,
				IHS_Number:  input.IHS_Number,
			}
			if _, err := up.CreateData(createPharmacist, &event, tx); err != nil {
				return err
			}
		case ero.EPCLab:
			createLaborant := el.CreateDto{
				Code:        input.Code,
				Employee_Id: &employeeData.Id,
				IHS_Number:  input.IHS_Number,
			}
			if _, err := ul.CreateData(createLaborant, &event, tx); err != nil {
				return err
			}
		case ero.EPCMwi:
			createMidWife := emw.CreateDto{
				Code:        input.Code,
				Employee_Id: &employeeData.Id,
				IHS_Number:  input.IHS_Number,
			}
			if _, err := umw.CreateData(createMidWife, &event, tx); err != nil {
				return err
			}
		case ero.EPCReg, ero.EPCScr:
			// do nothing
		default:
			return errors.New("invalid employee position")
		}

		mwRunner.setMwType(pu.MWTPost)
		// Run post-middleware
		if err := mwRunner.RunCreateMiddleware(createPostMw, &input, &data); err != nil {
			return err
		}

		pl.SetLogInfo(&event, nil, "complete")

		return nil
	})

	if err != nil {
		return nil, err
	}

	return &d.Data{
		Meta: d.II{
			"source":    source,
			"structure": "single-data",
			"status":    "created",
		},
		Data: data.ToResponse(),
	}, nil
}

func ReadList(input e.ReadListDto) (*d.Data, error) {
	var data *e.User
	var dataList []e.User
	var metaList *e.MetaDto
	var err error

	event := pl.Event{
		Feature: "ReadList",
		Source:  source,
	}

	// Start log
	pl.SetLogInfo(&event, input, "started", "readList")

	err = dg.I.Transaction(func(tx *gorm.DB) error {
		mwRunner := newMiddlewareRunner(&event, tx)
		mwRunner.setMwType(pu.MWTPre)
		// Run pre-middleware
		if err := mwRunner.RunReadListMiddleware(readListPreMw, &input, data); err != nil {
			return err
		}

		if dataList, metaList, err = ReadListData(input, &event, tx); err != nil {
			return err
		}

		mwRunner.setMwType(pu.MWTPost)
		// Run post-middleware
		if err := mwRunner.RunReadListMiddleware(readListPostMw, &input, data); err != nil {
			return err
		}

		return nil
	})

	if err != nil {
		return nil, err
	}

	return &d.Data{
		Meta: d.IS{
			"source":              source,
			"structure":           "list-data",
			"status":              "fetched",
			"page_number":         strconv.Itoa(metaList.PageNumber),
			"page_size":           strconv.Itoa(metaList.PageSize),
			"record_totalCount":   strconv.Itoa(metaList.Count),
			"record_currentCount": strconv.Itoa(len(dataList)),
		},
		Data: e.ToResponseList(dataList),
	}, nil
}

func ReadDetail(input e.ReadDetailDto) (*d.Data, error) {
	var data *e.User
	var err error

	event := pl.Event{
		Feature: "ReadDetail",
		Source:  source,
	}

	// Start log
	pl.SetLogInfo(&event, input, "started", "readDetail")

	err = dg.I.Transaction(func(tx *gorm.DB) error {
		mwRunner := newMiddlewareRunner(&event, tx)
		mwRunner.setMwType(pu.MWTPre)
		// Run pre-middleware
		if err := mwRunner.RunReadDetailMiddleware(readDetailPreMw, &input, data); err != nil {
			return err
		}

		if data, err = ReadDetailData(input, &event, tx); err != nil {
			return err
		}

		mwRunner.setMwType(pu.MWTPost)
		// Run post-middleware
		if err := mwRunner.RunReadDetailMiddleware(readDetailPostMw, &input, data); err != nil {
			return err
		}

		return nil
	})

	if err != nil {
		return nil, err
	}

	return &d.Data{
		Meta: d.IS{
			"source":    source,
			"structure": "single-data",
			"status":    "fetched",
		},
		Data: data.ToResponse(),
	}, nil
}

func Update(input e.UpdateDto) (*d.Data, error) {
	rdDto := e.ReadDetailDto{Id: input.Id}
	var data *e.User
	var err error

	event := pl.Event{
		Feature: "Update",
		Source:  source,
	}

	// Start log
	pl.SetLogInfo(&event, input, "started", "update")

	err = dg.I.Transaction(func(tx *gorm.DB) error {
		pl.SetLogInfo(&event, rdDto, "started", "DBReadDetail")
		if data, err = ReadDetailData(rdDto, &event, tx); err != nil {
			return err
		}

		mwRunner := newMiddlewareRunner(&event, tx)
		mwRunner.setMwType(pu.MWTPre)
		// Run pre-middleware
		if err := mwRunner.RunUpdateMiddleware(readDetailPreMw, &rdDto, data); err != nil {
			return err
		}

		person_id, err := getPersonIdByUserId(data.Id, input.ContractPosition_Code, &event, tx)
		if err != nil {
			return err
		} else {
			input.Person.Id = *person_id
		}

		if person_id, err := upe.CreateOrUpdatePerson(input.Person, &event, tx); err != nil {
			return err
		} else {
			input.Person_Id = person_id
		}

		for idx := range input.PersonAddresses {
			input.PersonAddresses[idx].Person_Id = *input.Person_Id
		}
		if err := upa.CreateOrUpdateBatch(input.PersonAddresses, &event, tx); err != nil {
			return err
		}

		for idx := range input.PersonContacts {
			input.PersonContacts[idx].Person_Id = *input.Person_Id
		}
		if err := upc.CreateOrUpdateBatch(input.PersonContacts, &event, tx); err != nil {
			return err
		}

		if err := UpdateData(input, data, &event, tx); err != nil {
			return err
		}

		if input.ContractPosition_Code == ero.CSCInt {
			readInt := esi.ReadDetailDto{User_Id: &data.Id}
			readIntData, err := usi.ReadDetailData(readInt, &event, tx)
			if err != nil {
				return err
			}
			createInt := esi.CreateDto{
				User_Id:            &data.Id,
				Person_Id:          input.Person_Id,
				Specialist_Code:    input.Specialist_Code,
				Subspecialist_Code: input.Subspecialist_Code,
			}
			if readIntData != nil {
				if err := usi.UpdateData(esi.UpdateDto{CreateDto: createInt}, readIntData, &event, tx); err != nil {
					return err
				}
				return nil
			}

			if _, err := usi.CreateData(createInt, &event, tx); err != nil {
				return err
			}
		}

		input.Employee.User_Id = &data.Id
		input.Employee.Person_Id = input.Person_Id
		employeeData, err := ue.ReadDetailData(ee.ReadDetailDto{User_Id: &data.Id}, &event, tx)
		if err != nil {
			return err
		}

		err = ue.UpdateData(setDataEmployeeUpdate(*input.Employee), employeeData, &event, tx)
		if err != nil {
			return err
		}

		switch input.Employee.Position_Code {
		case ero.EPCDoc:
			readDoc := ed.ReadDetailDto{Employee_Id: &employeeData.Id}
			readDocData, err := ud.ReadDetailData(readDoc, &event, tx)
			if err != nil {
				return err
			}
			createDoc := ed.CreateDto{
				Code:               input.Code,
				Employee_Id:        &employeeData.Id,
				IHS_Number:         input.IHS_Number,
				SIP_Number:         input.SIP_Number,
				Unit_Code:          input.Unit_Code,
				Specialist_Code:    input.Specialist_Code,
				Subspecialist_Code: input.Subspecialist_Code,
			}
			if readDocData != nil {
				if err := ud.UpdateData(ed.UpdateDto{CreateDto: createDoc}, readDocData, &event, tx); err != nil {
					return err
				}
				return nil
			}

			if _, err := ud.CreateData(createDoc, &event, tx); err != nil {
				return err
			}
		case ero.EPCNur:
			readNur := en.ReadDetailDto{Employee_Id: &employeeData.Id}
			readNurData, err := un.ReadDetailData(readNur, &event, tx)
			if err != nil {
				return err
			}
			createNur := en.CreateDto{
				Code:        input.Code,
				Employee_Id: &employeeData.Id,
				IHS_Number:  input.IHS_Number,
				Unit_Code:   input.Unit_Code,
				Infra_Code:  input.Infra_Code,
			}
			if readNurData != nil {
				if err := un.UpdateData(en.UpdateDto{CreateDto: createNur}, readNurData, &event, tx); err != nil {
					return err
				}
				return nil
			}

			if _, err := un.CreateData(createNur, &event, tx); err != nil {
				return err
			}
		case ero.EPCNut:
			readNut := et.ReadDetailDto{Employee_Id: &employeeData.Id}
			readNutData, err := ut.ReadDetailData(readNut, &event, tx)
			if err != nil {
				return err
			}
			createNut := et.CreateDto{
				Code:        input.Code,
				Employee_Id: &employeeData.Id,
				IHS_Number:  input.IHS_Number,
			}
			if readNutData != nil {
				if err := ut.UpdateData(et.UpdateDto{CreateDto: createNut}, readNutData, &event, tx); err != nil {
					return err
				}
				return nil
			}

			if _, err := ut.CreateData(createNut, &event, tx); err != nil {
				return err
			}
		case ero.EPCPha:
			readPha := ep.ReadDetailDto{Employee_Id: &employeeData.Id}
			readPhaData, err := up.ReadDetailData(readPha, &event, tx)
			if err != nil {
				return err
			}
			createPha := ep.CreateDto{
				Code:        input.Code,
				Employee_Id: &employeeData.Id,
				IHS_Number:  input.IHS_Number,
			}
			if readPhaData != nil {
				if err := up.UpdateData(ep.UpdateDto{CreateDto: createPha}, readPhaData, &event, tx); err != nil {
					return err
				}
				return nil
			}

			if _, err := up.CreateData(createPha, &event, tx); err != nil {
				return err
			}
		case ero.EPCLab:
			readLab := el.ReadDetailDto{Employee_Id: &employeeData.Id}
			readLabData, err := ul.ReadDetailData(readLab, &event, tx)
			if err != nil {
				return err
			}
			createLab := el.CreateDto{
				Code:        input.Code,
				Employee_Id: &employeeData.Id,
				IHS_Number:  input.IHS_Number,
			}
			if readLabData != nil {
				if err := ul.UpdateData(el.UpdateDto{CreateDto: createLab}, readLabData, &event, tx); err != nil {
					return err
				}
				return nil
			}

			if _, err := ul.CreateData(createLab, &event, tx); err != nil {
				return err
			}
		case ero.EPCMwi:
			readMidWife := emw.ReadDetailDto{Employee_Id: &employeeData.Id}
			readMidWifeData, err := umw.ReadDetailData(readMidWife, &event, tx)
			if err != nil {
				return err
			}
			createMidWife := emw.CreateDto{
				Code:        input.Code,
				Employee_Id: &employeeData.Id,
				IHS_Number:  input.IHS_Number,
			}
			if readMidWifeData != nil {
				if err := umw.UpdateData(emw.UpdateDto{CreateDto: createMidWife}, readMidWifeData, &event, tx); err != nil {
					return err
				}
				return nil
			}

			if _, err := umw.CreateData(createMidWife, &event, tx); err != nil {
				return err
			}
		case ero.EPCReg, ero.EPCScr:
			// do nothing
		default:
			return errors.New("invalid employee position")
		}

		pl.SetLogInfo(&event, nil, "complete")

		mwRunner.setMwType(pu.MWTPost)
		// Run post-middleware
		if err := mwRunner.RunUpdateMiddleware(readDetailPostMw, &rdDto, data); err != nil {
			return err
		}

		return nil
	})

	if err != nil {
		return nil, err
	}

	return &d.Data{
		Meta: d.IS{
			"source":    source,
			"structure": "single-data",
			"status":    "updated",
		},
		Data: data.ToResponse(),
	}, nil

}

func Delete(input e.DeleteDto) (*d.Data, error) {
	rdDto := e.ReadDetailDto{Id: input.Id}
	var data *e.User
	var err error

	event := pl.Event{
		Feature: "Delete",
		Source:  source,
	}

	// Start log
	pl.SetLogInfo(&event, input, "started", "delete")

	err = dg.I.Transaction(func(tx *gorm.DB) error {
		pl.SetLogInfo(&event, rdDto, "started", "DBReadDetail")
		if data, err = ReadDetailData(rdDto, &event, tx); err != nil {
			return err
		}

		mwRunner := newMiddlewareRunner(&event, tx)
		mwRunner.setMwType(pu.MWTPre)
		// Run pre-middleware
		if err := mwRunner.RunDeleteMiddleware(readDetailPreMw, &rdDto, data); err != nil {
			return err
		}

		if err := DeleteData(data, &event, tx); err != nil {
			return err
		}

		mwRunner.setMwType(pu.MWTPost)
		// Run post-middleware
		if err := mwRunner.RunDeleteMiddleware(readDetailPostMw, &rdDto, data); err != nil {
			return err
		}

		return nil
	})

	if err != nil {
		return nil, err
	}

	return &d.Data{
		Meta: d.IS{
			"source":    source,
			"structure": "single-data",
			"status":    "deleted",
		},
		Data: data.ToResponse(),
	}, nil

}

func Block(input e.ReadDetailDto) (*d.Data, error) {
	rdDto := e.ReadDetailDto{Id: input.Id}
	var data *e.User
	var err error

	event := pl.Event{
		Feature: "Block",
		Source:  source,
	}

	// Start log
	pl.SetLogInfo(&event, input, "started", "update")

	err = dg.I.Transaction(func(tx *gorm.DB) error {
		pl.SetLogInfo(&event, rdDto, "started", "DBReadDetail")
		if data, err = ReadDetailData(rdDto, &event, tx); err != nil {
			return err
		}

		if data != nil {
			pl.SetLogInfo(&event, rdDto, "started", "DBUpdate")
			data.Status_Code = erc.USCBlocked
			if err := tx.Save(&data).Error; err != nil {
				return err
			}
		}

		pl.SetLogInfo(&event, nil, "complete")
		return nil
	})

	if err != nil {
		return nil, err
	}

	return &d.Data{
		Meta: d.IS{
			"source":    source,
			"structure": "single-data",
			"status":    "updated",
		},
		Data: data.ToResponse(),
	}, nil

}

func Active(input e.ReadDetailDto) (*d.Data, error) {
	rdDto := e.ReadDetailDto{Id: input.Id}
	var data *e.User
	var err error

	event := pl.Event{
		Feature: "Active",
		Source:  source,
	}

	// Start log
	pl.SetLogInfo(&event, input, "started", "update")

	err = dg.I.Transaction(func(tx *gorm.DB) error {
		pl.SetLogInfo(&event, rdDto, "started", "DBReadDetail")
		if data, err = ReadDetailData(rdDto, &event, tx); err != nil {
			return err
		}

		if data != nil {
			pl.SetLogInfo(&event, rdDto, "started", "DBUpdate")
			data.Status_Code = erc.USCActive
			if err := tx.Save(&data).Error; err != nil {
				return err
			}
		}

		pl.SetLogInfo(&event, nil, "complete")
		return nil
	})

	if err != nil {
		return nil, err
	}

	return &d.Data{
		Meta: d.IS{
			"source":    source,
			"structure": "single-data",
			"status":    "updated",
		},
		Data: data.ToResponse(),
	}, nil

}
