"""Type definitions for fishinfo SDK.

This module contains legacy dataclass types for backwards compatibility.
For new code, prefer using the Pydantic models from fishinfo.models.
"""

from dataclasses import dataclass
from datetime import datetime
from enum import Enum
from typing import Any


class DataType(str, Enum):
    """Telemetry data types."""

    POSITION = "position"
    DEPTH = "depth"
    SPEED = "speed"
    HEADING = "heading"
    COURSE = "course"
    RATE_OF_TURN = "rate_of_turn"
    WATER_TEMPERATURE = "water_temperature"
    AIR_TEMPERATURE = "air_temperature"
    WIND_SPEED = "wind_speed"
    WIND_DIRECTION = "wind_direction"
    BAROMETRIC_PRESSURE = "barometric_pressure"
    HUMIDITY = "humidity"
    ENGINE = "engine"
    FUEL = "fuel"
    CATCH = "catch"
    AIS = "ais"
    ALARM = "alarm"
    CUSTOM = "custom"


@dataclass
class TelemetryPoint:
    """A single telemetry data point."""

    time: datetime
    data_type: DataType | str
    latitude: float | None = None
    longitude: float | None = None
    value: float | None = None
    value_json: Any | None = None
    source: str | None = None
    sentence_type: str | None = None
    raw_sentence: str | None = None
    quality: int | None = None
    satellites: int | None = None
    hdop: float | None = None
    metadata: dict[str, str] | None = None

    def to_dict(self) -> dict[str, Any]:
        """Convert to dictionary for serialization."""
        data: dict[str, Any] = {
            "time": self.time.isoformat(),
            "data_type": str(self.data_type),
        }
        if self.latitude is not None:
            data["latitude"] = self.latitude
        if self.longitude is not None:
            data["longitude"] = self.longitude
        if self.value is not None:
            data["value"] = self.value
        if self.value_json is not None:
            data["value_json"] = self.value_json
        if self.source is not None:
            data["source"] = self.source
        if self.sentence_type is not None:
            data["sentence_type"] = self.sentence_type
        if self.raw_sentence is not None:
            data["raw_sentence"] = self.raw_sentence
        if self.quality is not None:
            data["quality"] = self.quality
        if self.satellites is not None:
            data["satellites"] = self.satellites
        if self.hdop is not None:
            data["hdop"] = self.hdop
        if self.metadata is not None:
            data["metadata"] = self.metadata
        return data


@dataclass
class EngineData:
    """Structured engine data."""

    engine_id: int
    rpm: float | None = None
    oil_pressure_kpa: float | None = None
    oil_temperature_c: float | None = None
    coolant_temperature_c: float | None = None
    fuel_rate_lph: float | None = None
    hours: float | None = None
    load_percent: float | None = None

    def to_dict(self) -> dict[str, Any]:
        """Convert to dictionary."""
        data: dict[str, Any] = {"engine_id": self.engine_id}
        if self.rpm is not None:
            data["rpm"] = self.rpm
        if self.oil_pressure_kpa is not None:
            data["oil_pressure_kpa"] = self.oil_pressure_kpa
        if self.oil_temperature_c is not None:
            data["oil_temperature_c"] = self.oil_temperature_c
        if self.coolant_temperature_c is not None:
            data["coolant_temperature_c"] = self.coolant_temperature_c
        if self.fuel_rate_lph is not None:
            data["fuel_rate_lph"] = self.fuel_rate_lph
        if self.hours is not None:
            data["hours"] = self.hours
        if self.load_percent is not None:
            data["load_percent"] = self.load_percent
        return data


@dataclass
class CatchData:
    """Structured catch data."""

    event: str
    species_code: str | None = None
    species_name: str | None = None
    weight_kg: float | None = None
    count: int | None = None
    gear_type: str | None = None
    haul_number: int | None = None

    def to_dict(self) -> dict[str, Any]:
        """Convert to dictionary."""
        data: dict[str, Any] = {"event": self.event}
        if self.species_code is not None:
            data["species_code"] = self.species_code
        if self.species_name is not None:
            data["species_name"] = self.species_name
        if self.weight_kg is not None:
            data["weight_kg"] = self.weight_kg
        if self.count is not None:
            data["count"] = self.count
        if self.gear_type is not None:
            data["gear_type"] = self.gear_type
        if self.haul_number is not None:
            data["haul_number"] = self.haul_number
        return data
