package dto

import (
	"time"

	flightprepcheck "wucher/internal/domain/flight_prep_check"
	"wucher/internal/shared/pkg/uuidv7"
)

type FlightPrepCheckAttributes struct {
	ID                    string `json:"id,omitempty" example:"019d6772-471c-7b70-b42a-d94020ef61e4"`
	FlightInspectionID    string `json:"flight_inspection_id,omitempty" example:"019d6772-471c-7b70-b42a-d94020ef61e4"`
	NotamBriefing         bool   `json:"notam_briefing" example:"true"`
	WeatherBriefing       bool   `json:"weather_briefing" example:"false"`
	OperationalFlightPlan bool   `json:"operational_flight_plan" example:"true"`
	CreatedAt             string `json:"created_at,omitempty" example:"2026-04-09T05:00:00Z"`
	UpdatedAt             string `json:"updated_at,omitempty" example:"2026-04-09T05:00:00Z"`
}

type FlightPrepCheckResource struct {
	Type       string                    `json:"type" example:"flight_prep_check"`
	ID         string                    `json:"id,omitempty" example:"019d6772-471c-7b70-b42a-d94020ef61e4"`
	Attributes FlightPrepCheckAttributes `json:"attributes"`
}

type FlightPrepCheckResponse struct {
	Data FlightPrepCheckResource `json:"data"`
}

type FlightPrepCheckUpsertRequest struct {
	Data struct {
		Type       string                          `json:"type" validate:"required,oneof=flight_prep_check_upsert flight_prep_check" example:"flight_prep_check_upsert"`
		Attributes FlightPrepCheckUpsertAttributes `json:"attributes" validate:"required"`
	} `json:"data" validate:"required"`
}

type FlightPrepCheckUpsertAttributes struct {
	NotamBriefing         *bool `json:"notam_briefing,omitempty" example:"true"`
	WeatherBriefing       *bool `json:"weather_briefing,omitempty" example:"true"`
	OperationalFlightPlan *bool `json:"operational_flight_plan,omitempty" example:"false"`
}

func ToFlightPrepCheckDTO(check *flightprepcheck.FlightPrepCheck) *FlightPrepCheckResource {
	if check == nil {
		return nil
	}

	idStr, _ := uuidv7.BytesToString(check.ID)
	flightInspectionIDStr, _ := uuidv7.BytesToString(check.FlightInspectionID)

	createdAt := ""
	updatedAt := ""
	if !check.CreatedAt.IsZero() {
		createdAt = check.CreatedAt.UTC().Format(time.RFC3339)
	}
	if !check.UpdatedAt.IsZero() {
		updatedAt = check.UpdatedAt.UTC().Format(time.RFC3339)
	}

	return &FlightPrepCheckResource{
		Type: "flight_prep_check",
		ID:   idStr,
		Attributes: FlightPrepCheckAttributes{
			ID:                    idStr,
			FlightInspectionID:    flightInspectionIDStr,
			NotamBriefing:         check.NOTAMBriefing,
			WeatherBriefing:       check.WeatherBriefing,
			OperationalFlightPlan: check.OperationalFlightPlan,
			CreatedAt:             createdAt,
			UpdatedAt:             updatedAt,
		},
	}
}
