package request

import "encoding/json"

type ReserveAcCreateAttributes struct {
	AircraftID string                              `json:"helicopter_id" validate:"required" example:"019d6771-5fb5-7337-837f-bc5ed85181a1"`
	FlightID   string                              `json:"flight_id" validate:"required" example:"019d6773-ccca-71dd-b9c7-8e6f531deb90"`
	Inspection ReserveAcInspectionCreateAttributes `json:"inspection" validate:"required"`
}

type ReserveAcInspectionCreateAttributes struct {
	InspectionDate  string                               `json:"inspection_date" validate:"required" example:"2026-04-15"`
	Before          ReserveAcBeforeInspectionAttributes  `json:"before" validate:"required"`
	Prepare         ReserveAcPrepareInspectionAttributes `json:"prepare" validate:"required"`
	FleetStatusFile ReserveAcFleetStatusFileAttributes   `json:"fleet_status_file,omitempty"`
}

type ReserveAcBeforeInspectionAttributes struct {
	OilEngineNR1Checked       *bool                        `json:"oil_engine_nr1_checked,omitempty"`
	OilEngineNR2Checked       *bool                        `json:"oil_engine_nr2_checked,omitempty"`
	HydraulicLHChecked        *bool                        `json:"hydraulic_lh_checked,omitempty"`
	HydraulicRHChecked        *bool                        `json:"hydraulic_rh_checked,omitempty"`
	OilTransmissionMGBChecked *bool                        `json:"oil_transmission_mgb_checked,omitempty"`
	OilTransmissionIGBChecked *bool                        `json:"oil_transmission_igb_checked,omitempty"`
	OilTransmissionTGBChecked *bool                        `json:"oil_transmission_tgb_checked,omitempty"`
	FuelAmount                *float64                     `json:"fuel_amount,omitempty"`
	FuelUnit                  *string                      `json:"fuel_unit,omitempty"`
	FuelAddedAmount           *float64                     `json:"fuel_added_amount,omitempty"`
	Note                      *string                      `json:"note,omitempty"`
	FileChecklist             []ReserveAcFileChecklistItem `json:"file_checklist,omitempty"`
}

type ReserveAcFileChecklistItem struct {
	HelicopterFileID string `json:"helicopter_file_id" validate:"required" example:"019d7000-aaaa-7bbb-8ccc-111111111111"`
	IsDone           bool   `json:"is_done" example:"true"`
}

type ReserveAcPrepareInspectionAttributes struct {
	NotamBriefing         *bool                        `json:"notam_briefing,omitempty"`
	WeatherBriefing       *bool                        `json:"weather_briefing,omitempty"`
	OperationalFlightPlan *bool                        `json:"operational_flight_plan,omitempty"`
	FileChecklist         []ReserveAcFileChecklistItem `json:"file_checklist,omitempty"`
}

type ReserveAcFleetStatusFileAttributes struct {
	FileChecklist []ReserveAcFileChecklistItem `json:"file_checklist,omitempty"`
}

type ReserveAcAfterFlightInspectionAttributes struct {
	OilEngineNR1Checked       *string                      `json:"oil_engine_nr1_checked,omitempty"`
	OilEngineNR2Checked       *string                      `json:"oil_engine_nr2_checked,omitempty"`
	HydraulicLHChecked        *string                      `json:"hydraulic_lh_checked,omitempty"`
	HydraulicRHChecked        *string                      `json:"hydraulic_rh_checked,omitempty"`
	OilTransmissionMGBChecked *string                      `json:"oil_transmission_mgb_checked,omitempty"`
	OilTransmissionIGBChecked *string                      `json:"oil_transmission_igb_checked,omitempty"`
	OilTransmissionTGBChecked *string                      `json:"oil_transmission_tgb_checked,omitempty"`
	Note                      *string                      `json:"note,omitempty"`
	FileChecklist             []ReserveAcFileChecklistItem `json:"file_checklist,omitempty"`
}

type ReserveAcAfterFlightInspectionUpsertData struct {
	Type       string                                   `json:"type" validate:"required,oneof=after_flight_inspection_upsert after_flight_inspection"`
	Attributes ReserveAcAfterFlightInspectionAttributes `json:"attributes" validate:"required"`
}

type ReserveAcAfterFlightInspectionUpsertRequest struct {
	Data ReserveAcAfterFlightInspectionUpsertData `json:"data" validate:"required"`
}

type LastFlightsInspectionUpsertAttributes struct {
	FlightInspectionID string `json:"flight_inspection_id" validate:"required"`
	ReserveAcAfterFlightInspectionAttributes
}

type LastFlightsInspectionCreateData struct {
	Type       string                                `json:"type" validate:"required,oneof=last_flights_inspection_create last_flights_inspection"`
	Attributes LastFlightsInspectionUpsertAttributes `json:"attributes" validate:"required"`
}

type LastFlightsInspectionCreateRequest struct {
	Data LastFlightsInspectionCreateData `json:"data" validate:"required"`
}

type LastFlightsInspectionUpdateData struct {
	Type       string                                `json:"type" validate:"required,oneof=last_flights_inspection_update last_flights_inspection"`
	Attributes LastFlightsInspectionUpsertAttributes `json:"attributes" validate:"required"`
}

type LastFlightsInspectionUpdateRequest struct {
	Data LastFlightsInspectionUpdateData `json:"data" validate:"required"`
}

type LastFlightsInspectionDeleteAttributes struct {
	FlightInspectionID string `json:"flight_inspection_id" validate:"required"`
}

type LastFlightsInspectionDeleteData struct {
	Type       string                                `json:"type" validate:"required,oneof=last_flights_inspection_delete last_flights_inspection"`
	Attributes LastFlightsInspectionDeleteAttributes `json:"attributes" validate:"required"`
}

type LastFlightsInspectionDeleteRequest struct {
	Data LastFlightsInspectionDeleteData `json:"data" validate:"required"`
}

type ReserveAcCreateData struct {
	Type       string                    `json:"type" validate:"required,oneof=reserve_ac_create reserve_ac" example:"reserve_ac_create"`
	Attributes ReserveAcCreateAttributes `json:"attributes" validate:"required"`
}

type ReserveAcCreateRequest struct {
	Data ReserveAcCreateData `json:"data" validate:"required"`
}

type ReserveAcUpdateAttributes struct {
	AircraftID *string                              `json:"helicopter_id,omitempty" example:"019d6771-5fb5-7337-837f-bc5ed85181a1"`
	FlightID   *string                              `json:"flight_id,omitempty" example:"019d6773-ccca-71dd-b9c7-8e6f531deb90"`
	Inspection *ReserveAcInspectionUpdateAttributes `json:"inspection,omitempty"`
}

type ReserveAcInspectionUpdateAttributes struct {
	InspectionDate *string                               `json:"inspection_date,omitempty" example:"2026-04-15"`
	Before         *ReserveAcBeforeInspectionAttributes  `json:"before,omitempty"`
	Prepare        *ReserveAcPrepareInspectionAttributes `json:"prepare,omitempty"`
}

type ReserveAcUpdateData struct {
	Type       string                    `json:"type" validate:"required,oneof=reserve_ac_update reserve_ac" example:"reserve_ac_update"`
	ID         string                    `json:"id" example:"019d6773-ccca-71dd-b9c7-8e6f531deb90"`
	Attributes ReserveAcUpdateAttributes `json:"attributes" validate:"required"`
}

type ReserveAcUpdateRequest struct {
	Data ReserveAcUpdateData `json:"data" validate:"required"`
}

type ReserveAcDeleteData struct {
	ID string `json:"id" validate:"required" example:"019d6773-ccca-71dd-b9c7-8e6f531deb90"`
}

type ReserveAcDeleteRequest struct {
	Data ReserveAcDeleteData `json:"data" validate:"required"`
}

type TakeoverCreateAttributes struct {
	BaseID                *string                              `json:"base_id,omitempty"`
	DutyDate              string                               `json:"duty_date" validate:"required"`
	Notes                 *string                              `json:"notes,omitempty"`
	Detail                *TakeoverRosterDetailsInput          `json:"roster_detail,omitempty"`
	AircraftID            *string                              `json:"helicopter_id,omitempty"`
	Inspection            *ReserveAcInspectionCreateAttributes `json:"inspection,omitempty"`
	Files                 []TakeoverCreateFileInput            `json:"files,omitempty" validate:"omitempty,dive"`
	EditedTemplateFileIDs []string                             `json:"edited_template_file_ids,omitempty" validate:"omitempty,dive,uuid"`
}

type TakeoverCreateData struct {
	Type       string                   `json:"type" validate:"required,oneof=takeover_create takeover"`
	Attributes TakeoverCreateAttributes `json:"attributes" validate:"required"`
}

type TakeoverCreateRequest struct {
	Data TakeoverCreateData `json:"data" validate:"required"`
}

type TakeoverCreateFileInput struct {
	FileID           string `json:"file_id,omitempty" validate:"omitempty,uuid" example:"019e8713-aa19-7fb7-a510-a0332dc69697"`
	UploadIntentUUID string `json:"upload_intent_uuid,omitempty" validate:"omitempty,uuid" example:"019e8713-aa19-7fb7-a510-a0332dc69697"`
	Name             string `json:"name,omitempty" example:"takeover-report.pdf"`
}

type TakeoverUpdateAttributes struct {
	BaseID       *string                              `json:"base_id,omitempty"`
	Bases        *TakeoverBaseRef                     `json:"bases,omitempty"`
	DutyDate     *string                              `json:"duty_date,omitempty"`
	Notes        *string                              `json:"notes,omitempty"`
	Detail       *TakeoverRosterDetailsInput          `json:"roster_detail,omitempty"`
	RosterDetail *TakeoverRosterDetailsInput          `json:"roster_details,omitempty"`
	AircraftID   *string                              `json:"helicopter_id,omitempty"`
	Inspection   *ReserveAcInspectionCreateAttributes `json:"inspection,omitempty"`
	Files        []TakeoverCreateFileInput            `json:"files,omitempty" validate:"omitempty,dive"`
	FilesAdd     []TakeoverCreateFileInput            `json:"files_add,omitempty" validate:"omitempty,dive"`
	FilesRemove  []string                             `json:"files_remove,omitempty" validate:"omitempty,dive,uuid"`
	FilesReplace []TakeoverCreateFileInput            `json:"files_replace,omitempty" validate:"omitempty,dive"`
}

type TakeoverUpdateData struct {
	Type       string                   `json:"type" validate:"required,oneof=takeover_update takeover"`
	ID         string                   `json:"id"`
	Attributes TakeoverUpdateAttributes `json:"attributes" validate:"required"`
}

type TakeoverUpdateRequest struct {
	Data TakeoverUpdateData `json:"data" validate:"required"`
}

func (a *TakeoverUpdateAttributes) UnmarshalJSON(data []byte) error {
	type alias TakeoverUpdateAttributes
	var raw struct {
		alias
		Bases        *TakeoverBaseRef            `json:"bases,omitempty"`
		RosterDetail *TakeoverRosterDetailsInput `json:"roster_details,omitempty"`
	}
	if err := json.Unmarshal(data, &raw); err != nil {
		return err
	}
	*a = TakeoverUpdateAttributes(raw.alias)
	if a.BaseID == nil && raw.Bases != nil && raw.Bases.ID != "" {
		id := raw.Bases.ID
		a.BaseID = &id
	}
	if a.Detail == nil && raw.RosterDetail != nil {
		a.Detail = raw.RosterDetail
	}
	return nil
}

type TakeoverBaseRef struct {
	ID string `json:"id,omitempty"`
}

type TakeoverRosterCrewInput struct {
	ID string `json:"id,omitempty" example:"018f3a5c-7a6f-7c2a-8d4a-4b5b1f6c8e9d"`
}

type TakeoverRosterPilotInput struct {
	ID               string `json:"id,omitempty" example:"018f3a5c-7a6f-7c2a-8d4a-4b5b1f6c8e9d"`
	FlightInstructor bool   `json:"flight_instructor,omitempty"`
	LineChecker      bool   `json:"line_checker,omitempty"`
	Supervisor       bool   `json:"supervisor,omitempty"`
	Examiner         bool   `json:"examiner,omitempty"`
	CoPilot          bool   `json:"co_pilot,omitempty"`
	CrewType         string `json:"crew_type,omitempty" example:"main" enums:"main,additional"`
}

type TakeoverRosterOtherPersonInput struct {
	// OtherPersonID — OPTIONAL master person id to reuse (from GET /contact/other).
	OtherPersonID string `json:"other_person_id,omitempty" example:"019d6771-5fb5-7337-837f-bc5ed85181a1"`
	Name          string `json:"name,omitempty" example:"Guest Person"`
	MobilePhone   string `json:"mobile_phone,omitempty" example:"+43-1234"`
	Email         string `json:"email,omitempty" example:"guest@example.com"`
}

type TakeoverRosterDetailsInput struct {
	Pilot       []TakeoverRosterPilotInput       `json:"pilot"`
	Doctor      []TakeoverRosterCrewInput        `json:"doctor"`
	Rescuer     []TakeoverRosterCrewInput        `json:"rescuer"`
	Helper      []TakeoverRosterCrewInput        `json:"helper"`
	OtherPerson []TakeoverRosterOtherPersonInput `json:"other_person"`
}
