package fmreport

import (
	"time"

	"gorm.io/gorm"

	"wucher/internal/shared/pkg/uuidv7"
)

// FleetHistory is the 1:1 snapshot of the flown helicopter's fleet status, frozen
// when an FM report is created at after-flight inspection. It is read-only
// afterwards so later complaints / maintenance changes do not alter a historical
// report. Next-inspection rows use fixed columns (A/F, ENG1, ENG2, MAIN1, MAIN2)
// mirroring the report layout.
type FleetHistory struct {
	ID         []byte `gorm:"type:binary(16);primaryKey;column:id"`
	FMReportID []byte `gorm:"type:binary(16);not null;uniqueIndex:uidx_fm_report_fleet_history_fm_report_id;column:fm_report_id"`

	HelicopterStatus string `gorm:"type:varchar(16);column:helicopter_status"`

	AirframeHours float64 `gorm:"type:decimal(12,2);not null;default:0;column:airframe_hours"`
	Engine1Hours      float64 `gorm:"type:decimal(12,2);not null;default:0;column:engine1_hours"`
	Engine2Hours      float64 `gorm:"type:decimal(12,2);not null;default:0;column:engine2_hours"`

	AFType    string `gorm:"type:varchar(255);column:af_type"`
	AFDue     string `gorm:"type:varchar(255);column:af_due"`
	AFExt     string `gorm:"type:varchar(255);column:af_ext"`
	Eng1Type  string `gorm:"type:varchar(255);column:eng1_type"`
	Eng1Due   string `gorm:"type:varchar(255);column:eng1_due"`
	Eng1Ext   string `gorm:"type:varchar(255);column:eng1_ext"`
	Eng2Type  string `gorm:"type:varchar(255);column:eng2_type"`
	Eng2Due   string `gorm:"type:varchar(255);column:eng2_due"`
	Eng2Ext   string `gorm:"type:varchar(255);column:eng2_ext"`
	Main1Type string `gorm:"type:varchar(255);column:main1_type"`
	Main1Due  string `gorm:"type:varchar(255);column:main1_due"`
	Main1Ext  string `gorm:"type:varchar(255);column:main1_ext"`
	Main2Type string `gorm:"type:varchar(255);column:main2_type"`
	Main2Due  string `gorm:"type:varchar(255);column:main2_due"`
	Main2Ext  string `gorm:"type:varchar(255);column:main2_ext"`

	EASAReleaseID              []byte     `gorm:"type:binary(16);column:easa_release_id"`
	EASAIsSigned               bool       `gorm:"not null;default:false;column:easa_is_signed"`
	EASADate                   *time.Time `gorm:"type:date;column:easa_date"`
	EASAACHours                *string    `gorm:"type:varchar(20);column:easa_ac_hours"`
	EASALocation               *string    `gorm:"type:varchar(255);column:easa_location"`
	EASAWONo                   *string    `gorm:"type:varchar(100);column:easa_wo_no"`
	EASAMaintManualRevAirframe *string    `gorm:"type:varchar(100);column:easa_maint_manual_rev_airframe"`
	EASAMaintManualRevEngine   *string    `gorm:"type:varchar(100);column:easa_maint_manual_rev_engine"`
	EASASignerID               *string    `gorm:"type:varchar(100);column:easa_signer_id"`
	EASAPermitNo               *string    `gorm:"type:varchar(100);column:easa_permit_no"`
	EASASignedAt               *time.Time `gorm:"type:datetime(3);column:easa_signed_at"`
	EASASignedBy               []byte     `gorm:"type:binary(16);column:easa_signed_by"`

	MCFID           []byte     `gorm:"type:binary(16);column:mcf_id"`
	MCFIsSigned     bool       `gorm:"not null;default:false;column:mcf_is_signed"`
	MCFDate         *time.Time `gorm:"type:date;column:mcf_date"`
	MCFAFHours      *string    `gorm:"type:varchar(20);column:mcf_af_hours"`
	MCFLandingCount *int       `gorm:"column:mcf_landing_count"`
	MCFSignedBy     []byte     `gorm:"type:binary(16);column:mcf_signed_by"`
	MCFSignedByName string     `gorm:"type:varchar(255);column:mcf_signed_by_name"`

	CreatedAt time.Time `gorm:"column:created_at"`
}

func (FleetHistory) TableName() string { return "fm_report_fleet_history" }

func (f *FleetHistory) BeforeCreate(_ *gorm.DB) error {
	if len(f.ID) == 0 {
		f.ID = uuidv7.MustBytes()
	}
	return nil
}

// HasEASA reports whether an EASA release was captured in this snapshot.
func (f *FleetHistory) HasEASA() bool { return f != nil && len(f.EASAReleaseID) == 16 }

// HasMCF reports whether an MCF was captured in this snapshot.
func (f *FleetHistory) HasMCF() bool { return f != nil && len(f.MCFID) == 16 }
