package api

import (
	"github.com/gofiber/fiber/v2"
	"github.com/valyala/fasthttp/fasthttpadaptor"

	samlauth "wucher/internal/auth"
	"wucher/internal/service"
	"wucher/internal/shared/pkg/metrics"
	"wucher/internal/transport/http/handlers"
	"wucher/internal/transport/http/middleware"
)

var metricsHandler = fasthttpadaptor.NewFastHTTPHandler(metrics.PromHTTPHandler())

type RouteServices struct {
	Auth  *service.AuthService
	Audit *service.AuditLogService
	WOPI  *service.WOPIService
}

type RouteHandlers struct {
	User                     *handlers.UserHandler
	Contact                  *handlers.ContactHandler
	Health                   *handlers.HealthHandler
	BuildInfo                *handlers.BuildInfoHandler
	Auth                     *handlers.AuthHandler
	Role                     *handlers.RoleHandler
	Helicopter               *handlers.HelicopterHandler
	HelicopterUsage          *handlers.HelicopterUsageHandler
	HelicopterFile           *handlers.HelicopterFileHandler
	Flight                   *handlers.FlightHandler
	FlightData               *handlers.FlightDataHandler
	Mission                  *handlers.MissionHandler
	FMReport                 *handlers.FMReportHandler
	ReserveAc                *handlers.ReserveAcHandler
	Takeover                 *handlers.TakeoverHandler
	OtherPerson              *handlers.OtherPersonHandler
	Facility                 *handlers.FacilityHandler
	Base                     *handlers.BaseHandler
	Medicine                 *handlers.MedicineHandler
	Vocation                 *handlers.VocationHandler
	Opc                      *handlers.OpcHandler
	ForcesPresent            *handlers.ForcesPresentHandler
	Hospital                 *handlers.HospitalHandler
	HealthInsuranceCompanies *handlers.HealthInsuranceCompaniesHandler
	FederalState             *handlers.FederalStateHandler
	ICAO                     *handlers.ICAOHandler
	Land                     *handlers.LandHandler
	MasterSettings           *handlers.MasterSettingsHandler
	Branding                 *handlers.BrandingHandler
	FileManagerFolder        *handlers.FileManagerFolderHandler
	FileManagerFile          *handlers.FileManagerFileHandler
	FileManagerAttachment    *handlers.FileManagerAttachmentHandler
	FileManagerEvents        *handlers.FileManagerEventsHandler
	HelicopterFileEvents     *handlers.HelicopterFileEventsHandler
	DutyRoster               *handlers.DutyRosterHandler
	AirRescuerChecklist      *handlers.AirRescuerChecklistHandler
	InsurancePatientData     *handlers.InsurancePatientDataHandler
	PatientData              *handlers.PatientDataHandler
	HEMSOperationalData      *handlers.HEMSOperationalDataHandler
	HEMSOperationCategory    *handlers.HEMSOperationCategoryHandler
	HEMSOperation            *handlers.HEMSOperationHandler
	Complaint                *handlers.ComplaintHandler
	EASARelease              *handlers.EASAReleaseHandler
	ActionSignoff            *handlers.ActionSignoffHandler
	MCF                      *handlers.MCFHandler
	DUL                      *handlers.DULHandler
	FleetStatus              *handlers.FleetStatusHandler
	SAMLAuth                 *samlauth.Handler
}

type RouteDependencies struct {
	Handlers RouteHandlers
	Services RouteServices
}

func RegisterRoutes(app *fiber.App, deps RouteDependencies) {
	h := deps.Handlers
	s := deps.Services

	app.Get("/health", h.Health.Handle)
	app.Get("/version", h.BuildInfo.Handle)
	app.Get("/api/version", h.BuildInfo.Handle)
	app.Get("/api/build-info", h.BuildInfo.Handle)
	app.Get("/metrics", func(c *fiber.Ctx) error {
		metricsHandler(c.Context())
		return nil
	})

	api := app.Group("/api")
	api.Use(AuditMiddleware(s.Audit))
	v1 := api.Group("/v1")

	registerAuthRoutes(v1, h.Auth, s.Auth, h.SAMLAuth)
	registerUserRoutes(v1, h.User, h.FileManagerEvents, middleware.AuthRequired(s.Auth), s.Auth)
	registerContactRoutes(v1, h.Contact, middleware.AuthRequired(s.Auth), s.Auth)
	registerRoleRoutes(v1, h.Role, middleware.AuthRequired(s.Auth), s.Auth)
	registerHelicopterRoutes(v1, h.Helicopter, middleware.AuthRequired(s.Auth), s.Auth)
	registerHelicopterUsageRoutes(v1, h.HelicopterUsage, middleware.AuthRequired(s.Auth), s.Auth)
	registerHelicopterFileRoutes(v1, h.HelicopterFile, h.HelicopterFileEvents, middleware.AuthRequired(s.Auth), s.Auth)
	registerFlightRoutes(v1, h.Flight, middleware.AuthRequired(s.Auth), s.Auth)
	registerFlightDataRoutes(v1, h.FlightData, middleware.AuthRequired(s.Auth), s.Auth)
	registerMissionRoutes(v1, h.Mission, h.FileManagerEvents, middleware.AuthRequired(s.Auth), s.Auth)
	registerFMReportRoutes(v1, h.FMReport, middleware.AuthRequired(s.Auth), s.Auth)
	registerReserveAcRoutes(v1, h.ReserveAc, h.Takeover, middleware.AuthRequired(s.Auth), s.Auth)
	registerTakeoverRoutes(v1, h.Takeover, middleware.AuthRequired(s.Auth), s.Auth)
	registerOtherPersonRoutes(v1, h.OtherPerson, middleware.AuthRequired(s.Auth), s.Auth)
	registerLastFlightsInspectionRoutes(v1, h.ReserveAc, middleware.AuthRequired(s.Auth), s.Auth)
	registerFacilityRoutes(v1, h.Facility, middleware.AuthRequired(s.Auth), s.Auth)
	registerBaseRoutes(v1, h.Base, h.FileManagerEvents, middleware.AuthRequired(s.Auth), s.Auth)
	registerMedicineRoutes(v1, h.Medicine, middleware.AuthRequired(s.Auth), s.Auth)
	registerVocationRoutes(v1, h.Vocation, middleware.AuthRequired(s.Auth), s.Auth)
	registerOpcRoutes(v1, h.Opc, middleware.AuthRequired(s.Auth), s.Auth)
	registerForcesPresentRoutes(v1, h.ForcesPresent, middleware.AuthRequired(s.Auth), s.Auth)
	registerHospitalRoutes(v1, h.Hospital, middleware.AuthRequired(s.Auth), s.Auth)
	registerHealthInsuranceCompaniesRoutes(v1, h.HealthInsuranceCompanies, middleware.AuthRequired(s.Auth), s.Auth)
	registerFederalStateRoutes(v1, h.FederalState, middleware.AuthRequired(s.Auth), s.Auth)
	registerICAORoutes(v1, h.ICAO, middleware.AuthRequired(s.Auth), s.Auth)
	registerLandRoutes(v1, h.Land, middleware.AuthRequired(s.Auth), s.Auth)
	registerMasterSettingsRoutes(v1, h.MasterSettings, middleware.AuthRequired(s.Auth), s.Auth)
	registerBrandingRoutes(v1, h.Branding)
	registerFileManagerRoutes(v1, h.FileManagerFolder, h.FileManagerFile, h.FileManagerAttachment, h.FileManagerEvents, middleware.AuthRequired(s.Auth), s.Auth)
	registerWOPIRoutes(app, h.Takeover, s.WOPI)
	registerDutyRosterRoutes(v1, h.DutyRoster, middleware.AuthRequired(s.Auth), s.Auth)
	registerAirRescuerChecklistRoutes(v1, h.AirRescuerChecklist, middleware.AuthRequired(s.Auth), s.Auth)
	registerInsurancePatientDataRoutes(v1, h.InsurancePatientData, middleware.AuthRequired(s.Auth), s.Auth)
	registerPatientDataRoutes(v1, h.PatientData, middleware.AuthRequired(s.Auth), s.Auth)
	registerHEMSOperationalDataRoutes(v1, h.HEMSOperationalData, middleware.AuthRequired(s.Auth), s.Auth)
	registerHEMSOperationCategoryRoutes(v1, h.HEMSOperationCategory, middleware.AuthRequired(s.Auth), s.Auth)
	registerHEMSOperationRoutes(v1, h.HEMSOperation, middleware.AuthRequired(s.Auth), s.Auth)
	registerComplaintRoutes(v1, h.Complaint, middleware.AuthRequired(s.Auth), s.Auth)
	registerEASAReleaseRoutes(v1, h.EASARelease, middleware.AuthRequired(s.Auth), s.Auth)
	registerActionSignoffRoutes(v1, h.ActionSignoff, middleware.AuthRequired(s.Auth), s.Auth)
	registerMCFRoutes(v1, h.MCF, middleware.AuthRequired(s.Auth), s.Auth)
	registerDULRoutes(v1, h.DUL, h.FileManagerEvents, middleware.AuthRequired(s.Auth), s.Auth)
	registerFleetStatusRoutes(v1, h.FleetStatus, middleware.AuthRequired(s.Auth), s.Auth)
	registerAuditRoutes(v1, handlers.NewAuditHandler(s.Audit), middleware.AuthRequired(s.Auth), s.Auth)
	registerSwaggerRoutes(app)
}
