# Flight Lifecycle Manager

## Interface Definition

**Interface Name**: `IFlightLifecycleManager`

### Interface Methods

```python
class IFlightLifecycleManager(ABC):
    # Flight Lifecycle
    @abstractmethod
    def create_flight(self, flight_data: FlightData) -> str:
        pass
    
    @abstractmethod
    def get_flight(self, flight_id: str) -> Optional[Flight]:
        pass
    
    @abstractmethod
    def get_flight_state(self, flight_id: str) -> Optional[FlightState]:
        pass
    
    @abstractmethod
    def delete_flight(self, flight_id: str) -> bool:
        pass
    
    @abstractmethod
    def update_flight_status(self, flight_id: str, status: FlightStatusUpdate) -> bool:
        pass
    
    # Waypoint Management
    @abstractmethod
    def update_waypoint(self, flight_id: str, waypoint_id: str, waypoint: Waypoint) -> bool:
        pass
    
    @abstractmethod
    def batch_update_waypoints(self, flight_id: str, waypoints: List[Waypoint]) -> BatchUpdateResult:
        pass
    
    @abstractmethod
    def get_flight_metadata(self, flight_id: str) -> Optional[FlightMetadata]:
        pass
    
    # Validation
    @abstractmethod
    def validate_waypoint(self, waypoint: Waypoint) -> ValidationResult:
        pass
    
    @abstractmethod
    def validate_geofence(self, geofence: Geofences) -> ValidationResult:
        pass
    
    @abstractmethod
    def validate_flight_continuity(self, waypoints: List[Waypoint]) -> ValidationResult:
        pass
    
    # API Delegation Methods (called by F01)
    @abstractmethod
    def queue_images(self, flight_id: str, batch: ImageBatch) -> BatchQueueResult:
        """Delegates to F05 Image Input Pipeline and triggers F02.2 Processing Engine."""
        pass
    
    @abstractmethod
    def handle_user_fix(self, flight_id: str, fix_data: UserFixRequest) -> UserFixResult:
        """Delegates to F02.2 Processing Engine to apply fix."""
        pass
    
    @abstractmethod
    def create_client_stream(self, flight_id: str, client_id: str) -> StreamConnection:
        """Delegates to F15 SSE Event Streamer."""
        pass
    
    @abstractmethod
    def convert_object_to_gps(self, flight_id: str, frame_id: int, pixel: Tuple[float, float]) -> GPSPoint:
        """Delegates to F13 Coordinate Transformer."""
        pass
    
    # System Initialization
    @abstractmethod
    def initialize_system(self) -> bool:
        pass
```

## Component Description

### Responsibilities
- **Component ID**: F02.1
- **Flight Lifecycle**: Manage flight creation, persistence, and deletion.
- **System Entry Point**: Main interface for the REST API (F01).
- **Resource Management**: Initializes system components and manages `F02.2 Flight Processing Engine` instances.
- **Data Validation**: Validates inputs before processing.
- **Satellite Prefetching**: Triggers F04 prefetching on flight creation.

### Scope
- CRUD operations for Flights and Waypoints.
- Coordination of system startup.
- Hand-off of active processing tasks to F02.2.
- Does **NOT** contain the processing loop or recovery logic.

## API Methods

### `create_flight(flight_data: FlightData) -> str`
**Description**: Creates a new flight, validates data, sets ENU origin, and triggers satellite prefetching.
**Called By**: F01 Flight API
**Processing Flow**:
1. Generate flight_id.
2. Validate geofences and waypoints.
3. Call F13.set_enu_origin(flight_id, start_gps).
4. Trigger F04.prefetch_route_corridor().
5. Save to F03 Flight Database.
6. Return flight_id.

### `queue_images(flight_id: str, batch: ImageBatch) -> BatchQueueResult`
**Description**: Queues images and ensures a Processing Engine is active for this flight.
**Processing Flow**:
1. Delegate to F05.queue_batch().
2. Retrieve or create instance of `F02.2 Flight Processing Engine` for this flight.
3. Trigger `engine.start_processing()` (async).
4. Return result.

### `handle_user_fix(flight_id: str, fix_data: UserFixRequest) -> UserFixResult`
**Description**: Forwards user fix to the active processing engine.
**Processing Flow**:
1. Get active engine for flight_id.
2. Call `engine.apply_user_fix(fix_data)`.
3. Return result.

### `initialize_system() -> bool`
**Description**: startup routine.
**Processing Flow**:
1. Load config (F17).
2. Initialize models (F16).
3. Initialize DB (F03).
4. Initialize Cache (F04).
5. Load Indexes (F08).

## Dependencies
- **F02.2 Flight Processing Engine**: Managed child component.
- **F03 Flight Database**: Persistence.
- **F04 Satellite Data Manager**: Prefetching.
- **F05 Image Input Pipeline**: Image queuing.
- **F13 Coordinate Transformer**: ENU origin setting.
- **F15 SSE Event Streamer**: Stream creation.
- **F17 Configuration Manager**: Config loading.

