package service

import (
	"context"

	fleethistory "wucher/internal/domain/fleet_history"
)

type FleetHistoryService struct{ repo fleethistory.Repository }

func NewFleetHistoryService(repo fleethistory.Repository) *FleetHistoryService {
	return &FleetHistoryService{repo: repo}
}

func (s *FleetHistoryService) Record(ctx context.Context, helicopterID []byte, entityType string, entityID []byte, action, message string, actor []byte) error {
	if s == nil || s.repo == nil || len(helicopterID) != 16 {
		return nil
	}
	return s.repo.Create(ctx, &fleethistory.FleetHistory{
		HelicopterID: append([]byte(nil), helicopterID...),
		EntityType:   entityType,
		EntityID:     append([]byte(nil), entityID...),
		Action:       action,
		Message:      message,
		Actor:        append([]byte(nil), actor...),
	})
}

func (s *FleetHistoryService) RecordByInspection(ctx context.Context, inspectionID []byte, entityType string, entityID []byte, action, message string, actor []byte) error {
	if s == nil || s.repo == nil || len(inspectionID) != 16 {
		return nil
	}
	helicopterID, err := s.repo.HelicopterIDByInspection(ctx, inspectionID)
	if err != nil {
		return err
	}
	return s.Record(ctx, helicopterID, entityType, entityID, action, message, actor)
}

func (s *FleetHistoryService) ListByHelicopter(ctx context.Context, helicopterID []byte, limit, offset int) ([]fleethistory.FleetHistory, int64, error) {
	return s.repo.ListByHelicopter(ctx, helicopterID, limit, offset)
}
