"""
Mock data voor Elephate CLI

Bevat dezelfde mock resultaten als de frontend webapp.
Gebaseerd op: frontend/composables/useMockProject.ts
"""

import random
from typing import List, Dict, Any
from dataclasses import dataclass


@dataclass
class Category:
    """STABU categorie definitie"""
    code: str
    name: str
    full_name: str


# STABU Categorieën
CATEGORIES: List[Category] = [
    Category("50", "HWA", "Hemelwaterafvoer"),
    Category("51", "Binnenriolering", "Binnenriolering"),
    Category("52", "Waterinstallaties", "Waterinstallaties"),
    Category("53", "Sanitair", "Sanitaire toestellen"),
    Category("54", "Brandbestrijding", "Brandbestrijdingsinstallaties"),
    Category("60", "Verwarming", "Verwarmingsinstallatie"),
    Category("61", "Ventilatie", "Ventilatieinstallatie"),
]


# Mock resultaten per categorie (gebaseerd op useMockProject.ts)
MOCK_RESULTS: Dict[str, Dict[str, Any]] = {
    "50": {
        "category": "50 HWA",
        "items": [
            {"code": "HWA-001", "stabu_code": "50.10.10", "description": "HWA buis 80mm grijs lg6", "quantity": 55, "unit": "m", "unit_price": 4.52, "total_price": 248.71, "labor_hours": 1.5},
            {"code": "HWA-002", "stabu_code": "50.10.20", "description": "HWA buis 100mm grijs lg6", "quantity": 120, "unit": "m", "unit_price": 6.89, "total_price": 826.80, "labor_hours": 2.0},
            {"code": "HWA-003", "stabu_code": "50.20.10", "description": "HWA bocht 80mm 45gr", "quantity": 24, "unit": "st", "unit_price": 3.45, "total_price": 82.80, "labor_hours": 0.5},
            {"code": "HWA-004", "stabu_code": "50.20.20", "description": "HWA bocht 100mm 87gr", "quantity": 48, "unit": "st", "unit_price": 5.67, "total_price": 272.16, "labor_hours": 0.5},
            {"code": "HWA-005", "stabu_code": "50.30.10", "description": "Dakdoorvoer plat dak 80mm", "quantity": 24, "unit": "st", "unit_price": 89.50, "total_price": 2148.00, "labor_hours": 1.5},
            {"code": "HWA-006", "stabu_code": "50.30.20", "description": "Dakdoorvoer plat dak 100mm", "quantity": 12, "unit": "st", "unit_price": 112.50, "total_price": 1350.00, "labor_hours": 1.5},
            {"code": "HWA-007", "stabu_code": "50.40.10", "description": "Bladenvanger 100mm", "quantity": 36, "unit": "st", "unit_price": 26.12, "total_price": 940.42, "labor_hours": 0.5},
        ],
        "summary": {
            "total_items": 26,
            "total_cost": 5868.89,
            "total_labor_hours": 7.0
        }
    },
    "51": {
        "category": "51 Binnenriolering",
        "items": [
            {"code": "BRI-001", "stabu_code": "51.10.10", "description": "PVC buis SN8 110mm", "quantity": 450, "unit": "m", "unit_price": 12.45, "total_price": 5602.50, "labor_hours": 45.0},
            {"code": "BRI-002", "stabu_code": "51.10.20", "description": "PVC buis SN8 125mm", "quantity": 280, "unit": "m", "unit_price": 15.67, "total_price": 4387.60, "labor_hours": 28.0},
            {"code": "BRI-003", "stabu_code": "51.10.30", "description": "PVC buis SN8 160mm", "quantity": 120, "unit": "m", "unit_price": 22.34, "total_price": 2680.80, "labor_hours": 12.0},
            {"code": "BRI-004", "stabu_code": "51.20.10", "description": "Ontstoppingsstuk 110mm", "quantity": 48, "unit": "st", "unit_price": 18.90, "total_price": 907.20, "labor_hours": 4.8},
            {"code": "BRI-005", "stabu_code": "51.20.20", "description": "Ontstoppingsstuk 125mm", "quantity": 24, "unit": "st", "unit_price": 24.50, "total_price": 588.00, "labor_hours": 2.4},
            {"code": "BRI-006", "stabu_code": "51.30.10", "description": "Vloerput RVS 150x150mm", "quantity": 72, "unit": "st", "unit_price": 145.00, "total_price": 10440.00, "labor_hours": 14.4},
            {"code": "BRI-007", "stabu_code": "51.30.20", "description": "Vloerput RVS 200x200mm", "quantity": 24, "unit": "st", "unit_price": 189.00, "total_price": 4536.00, "labor_hours": 4.8},
            {"code": "BRI-008", "stabu_code": "51.40.10", "description": "Stankafsluiter 110mm", "quantity": 96, "unit": "st", "unit_price": 34.56, "total_price": 3317.76, "labor_hours": 4.8},
            {"code": "BRI-009", "stabu_code": "51.40.20", "description": "Stankafsluiter 50mm", "quantity": 144, "unit": "st", "unit_price": 18.90, "total_price": 2721.60, "labor_hours": 2.9},
            {"code": "BRI-010", "stabu_code": "51.50.10", "description": "Beluchter 50mm", "quantity": 48, "unit": "st", "unit_price": 28.45, "total_price": 1365.60, "labor_hours": 1.0},
            {"code": "BRI-011", "stabu_code": "51.60.10", "description": "Aansluiting op hoofdriool", "quantity": 2, "unit": "st", "unit_price": 2299.09, "total_price": 4598.18, "labor_hours": 1.0},
        ],
        "summary": {
            "total_items": 62,
            "total_cost": 41145.24,
            "total_labor_hours": 121.0
        }
    },
    "52": {
        "category": "52 Waterinstallaties",
        "items": [
            {"code": "WAT-001", "stabu_code": "52.10.10", "description": "Koperen buis 15mm", "quantity": 1200, "unit": "m", "unit_price": 8.45, "total_price": 10140.00, "labor_hours": 24.0},
            {"code": "WAT-002", "stabu_code": "52.10.20", "description": "Koperen buis 22mm", "quantity": 450, "unit": "m", "unit_price": 12.78, "total_price": 5751.00, "labor_hours": 9.0},
            {"code": "WAT-003", "stabu_code": "52.10.30", "description": "Koperen buis 28mm", "quantity": 180, "unit": "m", "unit_price": 18.90, "total_price": 3402.00, "labor_hours": 3.6},
            {"code": "WAT-004", "stabu_code": "52.20.10", "description": "Isolatie 15mm - 13mm dik", "quantity": 1200, "unit": "m", "unit_price": 4.56, "total_price": 5472.00, "labor_hours": 12.0},
            {"code": "WAT-005", "stabu_code": "52.20.20", "description": "Isolatie 22mm - 13mm dik", "quantity": 450, "unit": "m", "unit_price": 5.89, "total_price": 2650.50, "labor_hours": 4.5},
            {"code": "WAT-006", "stabu_code": "52.30.10", "description": "Watermeter combi", "quantity": 24, "unit": "st", "unit_price": 245.00, "total_price": 5880.00, "labor_hours": 12.0},
            {"code": "WAT-007", "stabu_code": "52.30.20", "description": "Hoofdkraan 28mm", "quantity": 24, "unit": "st", "unit_price": 89.00, "total_price": 2136.00, "labor_hours": 4.8},
            {"code": "WAT-008", "stabu_code": "52.40.10", "description": "Boiler 80L elektrisch", "quantity": 24, "unit": "st", "unit_price": 489.00, "total_price": 11736.00, "labor_hours": 24.0},
            {"code": "WAT-009", "stabu_code": "52.40.20", "description": "Expansievat 12L", "quantity": 24, "unit": "st", "unit_price": 78.50, "total_price": 1884.00, "labor_hours": 2.4},
            {"code": "WAT-010", "stabu_code": "52.50.10", "description": "Drukregelaar 22mm", "quantity": 24, "unit": "st", "unit_price": 156.00, "total_price": 3744.00, "labor_hours": 4.8},
            {"code": "WAT-011", "stabu_code": "52.50.20", "description": "Mengkraan thermostatisch", "quantity": 48, "unit": "st", "unit_price": 289.00, "total_price": 13872.00, "labor_hours": 9.6},
            {"code": "WAT-012", "stabu_code": "52.60.10", "description": "Leidingdoorvoer brandwerend", "quantity": 96, "unit": "st", "unit_price": 234.56, "total_price": 22517.76, "labor_hours": 14.4},
            {"code": "WAT-013", "stabu_code": "52.70.10", "description": "Aansluiting op waterleiding", "quantity": 1, "unit": "st", "unit_price": 21472.80, "total_price": 21472.80, "labor_hours": 7.08},
        ],
        "summary": {
            "total_items": 74,
            "total_cost": 110658.06,
            "total_labor_hours": 132.18
        }
    },
    "53": {
        "category": "53 Sanitair",
        "items": [
            {"code": "SAN-001", "stabu_code": "53.10.10", "description": "Wandcloset wit keramiek", "quantity": 48, "unit": "st", "unit_price": 289.00, "total_price": 13872.00, "labor_hours": 24.0},
            {"code": "SAN-002", "stabu_code": "53.10.20", "description": "Inbouwreservoir UP320", "quantity": 48, "unit": "st", "unit_price": 245.00, "total_price": 11760.00, "labor_hours": 48.0},
            {"code": "SAN-003", "stabu_code": "53.10.30", "description": "Bedieningsplaat Sigma20", "quantity": 48, "unit": "st", "unit_price": 89.00, "total_price": 4272.00, "labor_hours": 4.8},
            {"code": "SAN-004", "stabu_code": "53.20.10", "description": "Wastafel 60cm wit", "quantity": 48, "unit": "st", "unit_price": 145.00, "total_price": 6960.00, "labor_hours": 24.0},
            {"code": "SAN-005", "stabu_code": "53.20.20", "description": "Wastafelkraan eengreeps", "quantity": 48, "unit": "st", "unit_price": 128.00, "total_price": 6144.00, "labor_hours": 9.6},
            {"code": "SAN-006", "stabu_code": "53.20.30", "description": "Sifon wastafel chroom", "quantity": 48, "unit": "st", "unit_price": 34.50, "total_price": 1656.00, "labor_hours": 4.8},
            {"code": "SAN-007", "stabu_code": "53.30.10", "description": "Douchebak 90x90cm", "quantity": 24, "unit": "st", "unit_price": 234.00, "total_price": 5616.00, "labor_hours": 24.0},
            {"code": "SAN-008", "stabu_code": "53.30.20", "description": "Doucheset thermostatisch", "quantity": 24, "unit": "st", "unit_price": 345.00, "total_price": 8280.00, "labor_hours": 12.0},
            {"code": "SAN-009", "stabu_code": "53.30.30", "description": "Douchedeur 90cm", "quantity": 24, "unit": "st", "unit_price": 456.00, "total_price": 10944.00, "labor_hours": 12.0},
            {"code": "SAN-010", "stabu_code": "53.40.10", "description": "Ligbad 170x75cm acryl", "quantity": 12, "unit": "st", "unit_price": 389.00, "total_price": 4668.00, "labor_hours": 12.0},
            {"code": "SAN-011", "stabu_code": "53.40.20", "description": "Badkraan thermostatisch", "quantity": 12, "unit": "st", "unit_price": 278.00, "total_price": 3336.00, "labor_hours": 6.0},
            {"code": "SAN-012", "stabu_code": "53.50.10", "description": "Keukenkraan eengreeps", "quantity": 24, "unit": "st", "unit_price": 189.00, "total_price": 4536.00, "labor_hours": 4.8},
            {"code": "SAN-013", "stabu_code": "53.50.20", "description": "Spoelbak RVS 80x50cm", "quantity": 24, "unit": "st", "unit_price": 234.00, "total_price": 5616.00, "labor_hours": 12.0},
            {"code": "SAN-014", "stabu_code": "53.60.10", "description": "Fonteinkraan chroom", "quantity": 24, "unit": "st", "unit_price": 78.50, "total_price": 1884.00, "labor_hours": 2.4},
            {"code": "SAN-015", "stabu_code": "53.60.20", "description": "Fontein 40x22cm", "quantity": 24, "unit": "st", "unit_price": 89.00, "total_price": 2136.00, "labor_hours": 4.8},
            {"code": "SAN-016", "stabu_code": "53.70.10", "description": "Spiegel met verlichting", "quantity": 48, "unit": "st", "unit_price": 189.00, "total_price": 9072.00, "labor_hours": 9.6},
            {"code": "SAN-017", "stabu_code": "53.70.20", "description": "Toiletaccessoireset 5-dlg", "quantity": 48, "unit": "st", "unit_price": 67.50, "total_price": 3240.00, "labor_hours": 4.8},
            {"code": "SAN-018", "stabu_code": "53.80.10", "description": "Wasmachinekraan", "quantity": 24, "unit": "st", "unit_price": 45.00, "total_price": 1080.00, "labor_hours": 2.4},
            {"code": "SAN-019", "stabu_code": "53.80.20", "description": "Waterontharder appartement", "quantity": 24, "unit": "st", "unit_price": 21014.85, "total_price": 21014.85, "labor_hours": 16.6},
        ],
        "summary": {
            "total_items": 288,
            "total_cost": 130086.45,
            "total_labor_hours": 239.60
        }
    },
    "54": {
        "category": "54 Brandbestrijding",
        "items": [
            {"code": "BRA-001", "stabu_code": "54.10.10", "description": "Brandslanghaspel 30m", "quantity": 12, "unit": "st", "unit_price": 345.00, "total_price": 4140.00, "labor_hours": 6.0},
            {"code": "BRA-002", "stabu_code": "54.10.20", "description": "Brandblusser poeder 6kg", "quantity": 24, "unit": "st", "unit_price": 89.00, "total_price": 2136.00, "labor_hours": 2.4},
            {"code": "BRA-003", "stabu_code": "54.20.10", "description": "Brandslangaansluiting", "quantity": 6, "unit": "st", "unit_price": 234.00, "total_price": 1404.00, "labor_hours": 3.0},
            {"code": "BRA-004", "stabu_code": "54.20.20", "description": "Brandweerschakelaar", "quantity": 2, "unit": "st", "unit_price": 654.99, "total_price": 1309.98, "labor_hours": 1.9},
        ],
        "summary": {
            "total_items": 7,
            "total_cost": 8989.98,
            "total_labor_hours": 13.30
        }
    },
    "60": {
        "category": "60 Verwarmingsinstallatie",
        "items": [
            {"code": "VER-001", "stabu_code": "60.10.10", "description": "Vloerverwarming mat 10m2", "quantity": 240, "unit": "st", "unit_price": 189.00, "total_price": 45360.00, "labor_hours": 240.0},
            {"code": "VER-002", "stabu_code": "60.10.20", "description": "Verdeler 8-groeps", "quantity": 24, "unit": "st", "unit_price": 456.00, "total_price": 10944.00, "labor_hours": 24.0},
            {"code": "VER-003", "stabu_code": "60.10.30", "description": "Thermostaatkop M30", "quantity": 192, "unit": "st", "unit_price": 34.50, "total_price": 6624.00, "labor_hours": 19.2},
            {"code": "VER-004", "stabu_code": "60.20.10", "description": "Warmtepomp lucht/water 8kW", "quantity": 24, "unit": "st", "unit_price": 5890.00, "total_price": 141360.00, "labor_hours": 192.0},
            {"code": "VER-005", "stabu_code": "60.20.20", "description": "Buffervat 200L", "quantity": 24, "unit": "st", "unit_price": 678.00, "total_price": 16272.00, "labor_hours": 24.0},
            {"code": "VER-006", "stabu_code": "60.30.10", "description": "Radiator type 22 - 600x1000", "quantity": 48, "unit": "st", "unit_price": 234.00, "total_price": 11232.00, "labor_hours": 48.0},
            {"code": "VER-007", "stabu_code": "60.30.20", "description": "Radiator type 22 - 600x1400", "quantity": 24, "unit": "st", "unit_price": 312.00, "total_price": 7488.00, "labor_hours": 24.0},
            {"code": "VER-008", "stabu_code": "60.30.30", "description": "Radiatorkraan thermostatisch", "quantity": 72, "unit": "st", "unit_price": 45.00, "total_price": 3240.00, "labor_hours": 14.4},
            {"code": "VER-009", "stabu_code": "60.40.10", "description": "CV buis 22mm", "quantity": 1200, "unit": "m", "unit_price": 12.45, "total_price": 14940.00, "labor_hours": 60.0},
            {"code": "VER-010", "stabu_code": "60.40.20", "description": "CV buis 28mm", "quantity": 450, "unit": "m", "unit_price": 18.90, "total_price": 8505.00, "labor_hours": 22.5},
            {"code": "VER-011", "stabu_code": "60.50.10", "description": "CV isolatie 22mm", "quantity": 1200, "unit": "m", "unit_price": 5.89, "total_price": 7068.00, "labor_hours": 12.0},
            {"code": "VER-012", "stabu_code": "60.50.20", "description": "CV isolatie 28mm", "quantity": 450, "unit": "m", "unit_price": 7.45, "total_price": 3352.50, "labor_hours": 4.5},
            {"code": "VER-013", "stabu_code": "60.60.10", "description": "Kamerthermostaat draadloos", "quantity": 24, "unit": "st", "unit_price": 2524.91, "total_price": 60597.79, "labor_hours": 176.73},
        ],
        "summary": {
            "total_items": 37,
            "total_cost": 332983.29,
            "total_labor_hours": 861.33
        }
    },
    "61": {
        "category": "61 Ventilatie",
        "items": [
            {"code": "VEN-001", "stabu_code": "61.10.10", "description": "WTW unit 300m3/h", "quantity": 24, "unit": "st", "unit_price": 1890.00, "total_price": 45360.00, "labor_hours": 96.0},
            {"code": "VEN-002", "stabu_code": "61.10.20", "description": "Bypass klep WTW", "quantity": 24, "unit": "st", "unit_price": 234.00, "total_price": 5616.00, "labor_hours": 12.0},
            {"code": "VEN-003", "stabu_code": "61.20.10", "description": "Spirobuis 125mm", "quantity": 480, "unit": "m", "unit_price": 18.90, "total_price": 9072.00, "labor_hours": 48.0},
            {"code": "VEN-004", "stabu_code": "61.20.20", "description": "Spirobuis 160mm", "quantity": 240, "unit": "m", "unit_price": 24.50, "total_price": 5880.00, "labor_hours": 24.0},
            {"code": "VEN-005", "stabu_code": "61.20.30", "description": "Spirobuis 200mm", "quantity": 120, "unit": "m", "unit_price": 32.00, "total_price": 3840.00, "labor_hours": 12.0},
            {"code": "VEN-006", "stabu_code": "61.30.10", "description": "Flexibele slang 80mm 3m", "quantity": 144, "unit": "st", "unit_price": 23.50, "total_price": 3384.00, "labor_hours": 14.4},
            {"code": "VEN-007", "stabu_code": "61.30.20", "description": "Flexibele slang 125mm 3m", "quantity": 48, "unit": "st", "unit_price": 34.50, "total_price": 1656.00, "labor_hours": 4.8},
            {"code": "VEN-008", "stabu_code": "61.40.10", "description": "Plafondventiel 125mm", "quantity": 144, "unit": "st", "unit_price": 45.00, "total_price": 6480.00, "labor_hours": 28.8},
            {"code": "VEN-009", "stabu_code": "61.40.20", "description": "Toevoerventiel 80mm", "quantity": 96, "unit": "st", "unit_price": 34.00, "total_price": 3264.00, "labor_hours": 19.2},
            {"code": "VEN-010", "stabu_code": "61.50.10", "description": "Dakdoorvoer WTW", "quantity": 24, "unit": "st", "unit_price": 345.00, "total_price": 8280.00, "labor_hours": 24.0},
            {"code": "VEN-011", "stabu_code": "61.50.20", "description": "Geveldoorvoer 160mm", "quantity": 24, "unit": "st", "unit_price": 189.00, "total_price": 4536.00, "labor_hours": 12.0},
            {"code": "VEN-012", "stabu_code": "61.60.10", "description": "Geluiddemper 125mm", "quantity": 48, "unit": "st", "unit_price": 89.00, "total_price": 4272.00, "labor_hours": 9.6},
            {"code": "VEN-013", "stabu_code": "61.60.20", "description": "Brandklep 125mm", "quantity": 24, "unit": "st", "unit_price": 234.00, "total_price": 5616.00, "labor_hours": 4.8},
            {"code": "VEN-014", "stabu_code": "61.70.10", "description": "CO2 sensor ruimte", "quantity": 24, "unit": "st", "unit_price": 5245.96, "total_price": 125359.09, "labor_hours": 240.13},
        ],
        "summary": {
            "total_items": 38,
            "total_cost": 231615.09,
            "total_labor_hours": 549.73
        }
    }
}


def get_mock_results(
    num_docs: int = 5,
    include_categories: List[str] = None,
    add_variation: bool = True
) -> Dict[str, Any]:
    """
    Genereer mock calculatie resultaten.

    Args:
        num_docs: Aantal verwerkte documenten (voor variatie)
        include_categories: Specifieke categorieën om te tonen (None = alle)
        add_variation: Voeg lichte random variatie toe aan bedragen

    Returns:
        Dict met resultaten per categorie en totalen
    """
    if include_categories is None:
        include_categories = list(MOCK_RESULTS.keys())

    results = {}
    total_items = 0
    total_cost = 0.0
    total_labor_hours = 0.0

    for cat_code in include_categories:
        if cat_code not in MOCK_RESULTS:
            continue

        cat_data = MOCK_RESULTS[cat_code].copy()

        if add_variation:
            # Voeg lichte variatie toe (±5%) voor realisme
            variation = 1.0 + (random.random() - 0.5) * 0.1
            cat_data["summary"]["total_cost"] *= variation
            cat_data["summary"]["total_labor_hours"] *= variation

        results[cat_code] = cat_data
        total_items += cat_data["summary"]["total_items"]
        total_cost += cat_data["summary"]["total_cost"]
        total_labor_hours += cat_data["summary"]["total_labor_hours"]

    return {
        "categories": results,
        "totals": {
            "total_items": total_items,
            "total_cost": round(total_cost, 2),
            "total_labor_hours": round(total_labor_hours, 2),
            "documents_processed": num_docs
        }
    }


def get_category_name(code: str) -> str:
    """Krijg de volledige naam van een categorie"""
    for cat in CATEGORIES:
        if cat.code == code:
            return f"{cat.code} {cat.name}"
    return f"{code} Onbekend"


def get_mock_token_usage(num_docs: int = 1, num_pages: int = 10) -> Dict[str, Any]:
    """
    Generate mock token usage for a calculation.

    Args:
        num_docs: Number of documents processed
        num_pages: Total pages processed

    Returns:
        Dict with token usage information
    """
    # Simulate token usage based on document count
    # ~1000 input tokens per doc + 500 per page
    base_input = 1000 * num_docs + 500 * num_pages
    # ~300 output tokens per doc + 100 per page
    base_output = 300 * num_docs + 100 * num_pages

    # Add some random variation (±10%)
    input_tokens = int(base_input * (0.9 + random.random() * 0.2))
    output_tokens = int(base_output * (0.9 + random.random() * 0.2))
    total_tokens = input_tokens + output_tokens

    # Mock cost calculation (approx GPT-4 pricing in EUR)
    # Input: ~€0.03 per 1K tokens, Output: ~€0.06 per 1K tokens
    cost = (input_tokens * 0.00003 + output_tokens * 0.00006)

    # Daily usage simulation (user has some prior usage)
    prior_usage = random.randint(1000, 5000)
    daily_limit = 10000  # Mock daily limit

    return {
        "calculation": {
            "input_tokens": input_tokens,
            "output_tokens": output_tokens,
            "total_tokens": total_tokens,
            "estimated_cost": round(cost, 4)
        },
        "daily_usage": {
            "used": prior_usage + total_tokens,
            "total": daily_limit,
            "percentage": round((prior_usage + total_tokens) / daily_limit * 100, 1),
            "reset_time": "morgen 00:00"
        }
    }
