package service

import (
	"context"
	"strings"
	"time"

	"wucher/internal/domain/flight"
	"wucher/internal/shared/pkg/missioncode"
)

type FlightService struct {
	repo flight.FlightRepository
}

func NewFlightService(repo flight.FlightRepository) *FlightService {
	return &FlightService{repo: repo}
}

func (s *FlightService) Create(ctx context.Context, row *flight.Flight) error {
	if row != nil {
		now := time.Now()
		prefix := missioncode.BuildPrefix(now)
		latest, err := s.repo.GetLatestMissionCodeByPrefix(ctx, prefix)
		if err != nil {
			return err
		}
		nextCode := missioncode.NextFromLatest(prefix, latest)
		row.MissionCode = &nextCode
	}
	return s.repo.Create(ctx, row)
}

func (s *FlightService) Update(ctx context.Context, row *flight.Flight) error {
	return s.repo.Update(ctx, row)
}

func (s *FlightService) Delete(ctx context.Context, id []byte, deletedBy []byte) error {
	return s.repo.Delete(ctx, id, deletedBy)
}

func (s *FlightService) GetByID(ctx context.Context, id []byte) (*flight.Flight, error) {
	return s.repo.GetByID(ctx, id)
}

func (s *FlightService) ListByUserID(ctx context.Context, userID []byte, limit, offset int) ([]flight.Flight, int64, error) {
	return s.repo.ListByUserID(ctx, userID, limit, offset)
}

func (s *FlightService) ListByCreatedBy(ctx context.Context, createdBy []byte, filter string, date string, sort string, limit, offset int) ([]flight.Flight, int64, error) {
	return s.repo.ListByCreatedBy(ctx, createdBy, filter, date, normalizeFlightSort(sort), limit, offset)
}

func (s *FlightService) GetByReserveAcID(ctx context.Context, reserveAcID []byte) (*flight.Flight, error) {
	return s.repo.GetByReserveAcID(ctx, reserveAcID)
}

func (s *FlightService) ListByReserveAcID(ctx context.Context, reserveAcID []byte) ([]flight.Flight, error) {
	return s.repo.ListByReserveAcID(ctx, reserveAcID)
}

func (s *FlightService) GetByTakeoverAcID(ctx context.Context, takeoverAcID []byte) (*flight.Flight, error) {
	return s.repo.GetByTakeoverAcID(ctx, takeoverAcID)
}

func (s *FlightService) ListByTakeoverAcIDs(ctx context.Context, takeoverAcIDs [][]byte) ([]flight.Flight, error) {
	return s.repo.ListByTakeoverAcIDs(ctx, takeoverAcIDs)
}

func (s *FlightService) GetByDutyRosterID(ctx context.Context, dutyRosterID []byte) (*flight.Flight, error) {
	return s.repo.GetByDutyRosterID(ctx, dutyRosterID)
}

func (s *FlightService) ListByDutyRosterIDs(ctx context.Context, dutyRosterIDs [][]byte) ([]flight.Flight, error) {
	return s.repo.ListByDutyRosterIDs(ctx, dutyRosterIDs)
}

func (s *FlightService) List(ctx context.Context, filter string, date string, sort string, limit, offset int) ([]flight.Flight, int64, error) {
	return s.repo.List(ctx, filter, date, normalizeFlightSort(sort), limit, offset)
}

func normalizeFlightSort(sort string) string {
	if normalized := normalizeSortByRules(strings.TrimSpace(sort),
		sortField("flights.mission_code", "mission_code"),
		sortField("flights.date", "date"),
		sortField("flights.created_at", "created_at"),
		sortField("flights.updated_at", "updated_at"),
	); normalized != "" {
		return normalized
	}
	return "flights.date DESC, flights.created_at DESC"
}
