package mysql

import (
	"context"
	"testing"
	"time"

	"wucher/internal/domain/base"
	"wucher/internal/domain/flight"
	flightinspection "wucher/internal/domain/flight_inspection"
	"wucher/internal/domain/helicopter"
	reserveac "wucher/internal/domain/reserve_ac"
	takeoverdomain "wucher/internal/domain/takeover"
	"wucher/internal/shared/pkg/uuidv7"
)

func TestFlightRepository_ListByCreatedBy_PreloadsTakeoverCompletionRelations(t *testing.T) {
	db := openDBTestSQLite(t)
	if err := db.AutoMigrate(
		&base.Base{},
		&helicopter.Helicopter{},
		&flightinspection.FlightInspection{},
		&reserveac.ReserveAc{},
		&takeoverdomain.TakeoverAc{},
		&takeoverdomain.TakeoverRosterCrew{},
		&takeoverdomain.TakeoverOtherPerson{},
		&flight.Flight{},
	); err != nil {
		t.Fatalf("auto migrate: %v", err)
	}
	if err := db.Exec(`CREATE TABLE users (
		id BLOB PRIMARY KEY,
		first_name TEXT,
		last_name TEXT
	)`).Error; err != nil {
		t.Fatalf("create users table: %v", err)
	}
	if err := db.Exec(`CREATE TABLE duty_rosters (
		id BLOB PRIMARY KEY,
		flight_id BLOB,
		deleted_at DATETIME NULL
	)`).Error; err != nil {
		t.Fatalf("create duty_rosters table: %v", err)
	}

	now := time.Now().UTC()
	baseID := uuidv7.MustBytes()
	heliID := uuidv7.MustBytes()
	inspectionID := uuidv7.MustBytes()
	reserveID := uuidv7.MustBytes()
	takeoverID := uuidv7.MustBytes()
	flightID := uuidv7.MustBytes()
	creatorID := uuidv7.MustBytes()

	if err := db.Create(&base.Base{ID: baseID, BaseName: "Base A", CreatedAt: now, UpdatedAt: now}).Error; err != nil {
		t.Fatalf("create base: %v", err)
	}
	if err := db.Create(&helicopter.Helicopter{ID: heliID, Designation: "H145", Identifier: "D-TEST", Type: "H145", CreatedAt: now, UpdatedAt: now}).Error; err != nil {
		t.Fatalf("create helicopter: %v", err)
	}
	if err := db.Create(&flightinspection.FlightInspection{ID: inspectionID, InspectionDate: now, Status: flightinspection.StatusDraft, CreatedAt: now, UpdatedAt: now}).Error; err != nil {
		t.Fatalf("create flight inspection: %v", err)
	}
	if err := db.Create(&reserveac.ReserveAc{
		ID:           reserveID,
		BaseID:       baseID,
		AircraftID:   heliID,
		InspectionID: inspectionID,
		Status:       reserveac.StatusApproved,
		CreatedAt:    now,
		UpdatedAt:    now,
	}).Error; err != nil {
		t.Fatalf("create reserve ac: %v", err)
	}
	if err := db.Create(&takeoverdomain.TakeoverAc{
		ID:          takeoverID,
		BaseID:      baseID,
		ReserveAcID: reserveID,
		CreatedAt:   now,
		UpdatedAt:   now,
		CreatedBy:   creatorID,
		UpdatedBy:   creatorID,
	}).Error; err != nil {
		t.Fatalf("create takeover: %v", err)
	}
	if err := db.Create(&takeoverdomain.TakeoverRosterCrew{
		ID:         uuidv7.MustBytes(),
		TakeoverID: takeoverID,
		RoleCode:   "pilot",
		CrewType:   "main",
		UserID:     uuidv7.MustBytes(),
		NameLabel:  "Pilot A",
		CreatedAt:  now,
		UpdatedAt:  now,
	}).Error; err != nil {
		t.Fatalf("create takeover crew: %v", err)
	}
	if err := db.Create(&flight.Flight{
		ID:           flightID,
		TakeoverAcID: takeoverID,
		Status:       "draft",
		Date:         now,
		CreatedAt:    now,
		CreatedBy:    creatorID,
		UpdatedAt:    now,
		UpdatedBy:    creatorID,
	}).Error; err != nil {
		t.Fatalf("create flight: %v", err)
	}

	repo := NewFlightRepository(db)
	rows, total, err := repo.ListByCreatedBy(context.Background(), creatorID, "", "", "", 10, 0)
	if err != nil {
		t.Fatalf("list by created by: %v", err)
	}
	if total != 1 || len(rows) != 1 {
		t.Fatalf("unexpected result size total=%d len=%d", total, len(rows))
	}
	row := rows[0]
	if row.Takeover == nil || row.Takeover.Base == nil || row.Takeover.ReserveAc == nil || row.Takeover.ReserveAc.Aircraft == nil || row.Takeover.ReserveAc.Inspection == nil {
		t.Fatalf("expected takeover relations to be preloaded: %#v", row.Takeover)
	}
	if len(row.Takeover.RosterCrews) == 0 {
		t.Fatalf("expected takeover roster crews to be preloaded")
	}
}
