package mcf

import (
	"time"

	"gorm.io/gorm"

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

const (
	ResultPassed = "passed"
	ResultFailed = "failed"
)

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

	Date         *time.Time `gorm:"type:date;column:date"`
	AFHours      string     `gorm:"type:varchar(20);column:af_hours"`
	LandingCount int        `gorm:"type:int;column:landing_count"`
	UTCtime      *time.Time `gorm:"type:datetime(3);column:utc_time"`

	DecidedBy []byte     `gorm:"type:binary(16);column:decided_by"`
	DecidedAt *time.Time `gorm:"type:datetime(3);column:decided_at"`

	Result      *string    `gorm:"type:varchar(20);column:result"`
	Notes       *string    `gorm:"type:text;column:notes"`
	CompletedAt *time.Time `gorm:"type:datetime(3);column:completed_at"`
	CompletedBy []byte     `gorm:"type:binary(16);column:completed_by"`

	// Cancelled marks a DRAFT MCF that was set to "No" without ever being signed — the
	// check flight never happened ("tidak jadi"). It releases the MCF status but the row
	// is kept as a cancelled record in the list. A signed MCF is a real event, not cancellable.
	CancelledAt *time.Time `gorm:"type:datetime(3);column:cancelled_at"`
	CancelledBy []byte     `gorm:"type:binary(16);column:cancelled_by"`

	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 (MaintenanceCheckFlight) TableName() string { return "maintenance_check_flights" }

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

func (m *MaintenanceCheckFlight) IsComplete() bool {
	return m != nil && m.CompletedAt != nil
}

func (m *MaintenanceCheckFlight) IsCancelled() bool {
	return m != nil && m.CancelledAt != nil
}

// IsOpen reports whether the MCF is an active (open) draft: not signed, not cancelled.
func (m *MaintenanceCheckFlight) IsOpen() bool {
	return m != nil && m.CompletedAt == nil && m.CancelledAt == nil
}

// IsCleared reports whether the MCF is done, releasing the aircraft from MCF status. Once
// the MCF is SIGNED (completed) it is considered done regardless of pass/fail — the result
// is recorded, but signing itself takes the helicopter out of MCF. A draft (not yet signed)
// is NOT cleared.
func (m *MaintenanceCheckFlight) IsCleared() bool {
	return m != nil && m.CompletedAt != nil
}

func (m *MaintenanceCheckFlight) IsPending() bool {
	return m != nil && !m.IsCleared()
}

func IsValidResult(s string) bool {
	switch s {
	case ResultPassed, ResultFailed:
		return true
	default:
		return false
	}
}
