package easarelease

import (
	"time"

	"gorm.io/gorm"

	complaint "wucher/internal/domain/complaint"
	flightdomain "wucher/internal/domain/flight"
	helicopter "wucher/internal/domain/helicopter"
	"wucher/internal/shared/pkg/uuidv7"
)

type EASARelease struct {
	ID           []byte `gorm:"type:binary(16);primaryKey;column:id"`
	HelicopterID []byte `gorm:"type:binary(16);not null;index;column:helicopter_id"`

	// ComplaintID scopes the release to the specific complaint it closes (optional).
	// When set, the release is fetched via get-by-complaint and closes that complaint.
	// When NULL the release is standalone (aircraft cleared with no open defect).
	ComplaintID []byte `gorm:"type:binary(16);index;column:complaint_id"`

	// FlightID — optional flight context. Retained for the standalone (no-complaint)
	// sign-off gate, which is still flight-scoped. Not required for complaint releases.
	FlightID []byte `gorm:"type:binary(16);index;column:flight_id"`

	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"`

	SignedAt *time.Time `gorm:"type:datetime(3);column:signed_at"`
	SignedBy []byte     `gorm:"type:binary(16);column:signed_by"`

	Helicopter *helicopter.Helicopter `gorm:"foreignKey:HelicopterID;references:ID"`
	Flight     *flightdomain.Flight   `gorm:"foreignKey:FlightID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
	Complaint  *complaint.Complaint   `gorm:"foreignKey:ComplaintID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`

	CreatedAt time.Time  `gorm:"type:datetime(3);column:created_at"`
	CreatedBy []byte     `gorm:"type:binary(16);column:created_by"`
	UpdatedAt time.Time  `gorm:"type:datetime(3);column:updated_at"`
	UpdatedBy []byte     `gorm:"type:binary(16);column:updated_by"`
	DeletedAt *time.Time `gorm:"type:datetime(3);index;column:deleted_at"`
	DeletedBy []byte     `gorm:"type:binary(16);column:deleted_by"`
}

func (EASARelease) TableName() string { return "easa_releases" }

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

func (e *EASARelease) IsSigned() bool { return e != nil && e.SignedAt != nil }

func (e *EASARelease) IsActive() bool { return e.IsSigned() }

// MissingSignFields reports which mandatory fields are still empty for signing.
// Only the signer is required — it is auto-filled from the duty pilot. The other
// document fields (date, A/C hours, location, WO no, manual revisions) are
// adjustable free text and optional, so a release can be signed without them.
func (e *EASARelease) MissingSignFields() []string {
	if e == nil {
		return []string{"easa_signer_id"}
	}
	var missing []string
	if e.EASASignerID == nil || *e.EASASignerID == "" {
		missing = append(missing, "easa_signer_id")
	}
	return missing
}
