"""
Data models for carbon footprint calculations.
Uses object-oriented design with dataclasses for clean, typed structures.
"""

from dataclasses import dataclass, field
from datetime import datetime
from typing import List, Optional
from enum import Enum


class EmissionCategory(Enum):
    """Categories of carbon emissions based on GHG Protocol scopes."""
    ELECTRICITY = "electricity"
    NATURAL_GAS = "natural_gas"
    TRANSPORTATION = "transportation"
    WASTE = "waste"
    WATER = "water"
    BUSINESS_TRAVEL = "business_travel"
    REFRIGERANTS = "refrigerants"


class EmissionScope(Enum):
    """GHG Protocol emission scopes."""
    SCOPE_1 = "Scope 1 - Direct Emissions"
    SCOPE_2 = "Scope 2 - Indirect Energy"
    SCOPE_3 = "Scope 3 - Other Indirect"


@dataclass
class EmissionRecord:
    """Represents a single emission record for a business activity."""
    category: EmissionCategory
    quantity: float
    unit: str
    date: str
    description: str = ""
    co2_kg: float = 0.0
    scope: EmissionScope = EmissionScope.SCOPE_1

    def to_dict(self) -> dict:
        """Convert emission record to dictionary."""
        return {
            "category": self.category.value,
            "quantity": self.quantity,
            "unit": self.unit,
            "date": self.date,
            "description": self.description,
            "co2_kg": self.co2_kg,
            "scope": self.scope.value,
        }

    @classmethod
    def from_dict(cls, data: dict) -> "EmissionRecord":
        """Create EmissionRecord from dictionary."""
        return cls(
            category=EmissionCategory(data["category"]),
            quantity=data["quantity"],
            unit=data["unit"],
            date=data["date"],
            description=data.get("description", ""),
            co2_kg=data.get("co2_kg", 0.0),
            scope=EmissionScope(data.get("scope", EmissionScope.SCOPE_1.value)),
        )


@dataclass
class BusinessProfile:
    """Profile information for a small business."""
    name: str
    industry: str
    employee_count: int
    location: str
    business_id: Optional[str] = None
    created_at: str = field(default_factory=lambda: datetime.now().isoformat())

    def to_dict(self) -> dict:
        """Convert business profile to dictionary."""
        return {
            "name": self.name,
            "industry": self.industry,
            "employee_count": self.employee_count,
            "location": self.location,
            "business_id": self.business_id,
            "created_at": self.created_at,
        }


@dataclass
class CarbonReport:
    """Generated carbon footprint report for a business."""
    business: BusinessProfile
    records: List[EmissionRecord]
    total_co2_kg: float
    total_co2_tonnes: float
    breakdown_by_category: dict
    breakdown_by_scope: dict
    period_start: str
    period_end: str
    recommendations: List[str] = field(default_factory=list)
    generated_at: str = field(default_factory=lambda: datetime.now().isoformat())

    def to_dict(self) -> dict:
        """Convert report to dictionary."""
        return {
            "business": self.business.to_dict(),
            "total_co2_kg": self.total_co2_kg,
            "total_co2_tonnes": self.total_co2_tonnes,
            "breakdown_by_category": self.breakdown_by_category,
            "breakdown_by_scope": self.breakdown_by_scope,
            "period_start": self.period_start,
            "period_end": self.period_end,
            "recommendations": self.recommendations,
            "generated_at": self.generated_at,
            "record_count": len(self.records),
        }
