#!/usr/bin/env python3
"""
Create Test Dataset with Known Devices

Uses Flipper Zero unit test files (known good captures with protocol names)
and adds GPS coordinates for testing RTL_433 integration.
"""

import os
import shutil
from pathlib import Path
import sys

# Add project to path
sys.path.insert(0, str(Path(__file__).parent.parent))

from src.parser.sub_parser import parse_sub_file

# Test dataset configuration
TEST_DATASET = [
    {
        "source": "signatures/flipperzero-firmware/applications/debug/unit_tests/resources/unit_tests/subghz/gate_tx.sub",
        "name": "GateTX_Gate_Opener",
        "gps": {"lat": 37.7749, "lon": -122.4194},  # San Francisco
        "description": "GateTX garage door opener (433.92 MHz)"
    },
    {
        "source": "signatures/flipperzero-firmware/applications/debug/unit_tests/resources/unit_tests/subghz/honeywell_wdb_raw.sub",
        "name": "Honeywell_Doorbell",
        "gps": {"lat": 40.7128, "lon": -74.0060},  # New York
        "description": "Honeywell wireless doorbell RAW capture (433.92 MHz)"
    },
    {
        "source": "signatures/flipperzero-firmware/applications/debug/unit_tests/resources/unit_tests/subghz/magellan.sub",
        "name": "Magellan_Sensor",
        "gps": {"lat": 34.0522, "lon": -118.2437},  # Los Angeles
        "description": "Magellan security sensor"
    },
    {
        "source": "signatures/flipperzero-firmware/applications/debug/unit_tests/resources/unit_tests/subghz/megacode.sub",
        "name": "Linear_MegaCode",
        "gps": {"lat": 41.8781, "lon": -87.6298},  # Chicago
        "description": "Linear MegaCode garage door"
    },
    {
        "source": "signatures/flipperzero-firmware/applications/debug/unit_tests/resources/unit_tests/subghz/holtek_ht12x_raw.sub",
        "name": "Holtek_Remote",
        "gps": {"lat": 29.7604, "lon": -95.3698},  # Houston
        "description": "Holtek HT12X remote control RAW"
    }
]


def create_test_dataset(output_dir="data/test_known_devices"):
    """Create test dataset with known devices and GPS coordinates"""

    output_path = Path(output_dir)
    output_path.mkdir(parents=True, exist_ok=True)

    print("="*70)
    print("Creating Test Dataset with Known Devices")
    print("="*70)
    print()

    successful = []
    failed = []

    for item in TEST_DATASET:
        source = Path(item["source"])

        if not source.exists():
            print(f"✗ {item['name']}: Source file not found")
            failed.append(item)
            continue

        try:
            # Parse to verify it's valid
            metadata = parse_sub_file(str(source))

            # Create filename with GPS
            lat = item["gps"]["lat"]
            lon = item["gps"]["lon"]
            lat_str = f"{abs(lat):.4f}{'N' if lat >= 0 else 'S'}"
            lon_str = f"{abs(lon):.4f}{'W' if lon < 0 else 'E'}"

            dest_filename = f"{lat_str}_{lon_str}_{item['name']}.sub"
            dest_path = output_path / dest_filename

            # Copy file
            shutil.copy2(source, dest_path)

            # Print info
            print(f"✓ {item['name']}")
            print(f"  Source: {source.name}")
            print(f"  Protocol: {metadata.protocol}")
            print(f"  Frequency: {metadata.frequency / 1_000_000:.3f} MHz")
            print(f"  Format: {metadata.file_format}")
            if metadata.has_raw_data:
                print(f"  RAW Data: {metadata.pulse_count} pulses")
            print(f"  GPS: {lat}, {lon}")
            print(f"  Saved: {dest_filename}")
            print(f"  Description: {item['description']}")
            print()

            successful.append({
                **item,
                "filename": dest_filename,
                "metadata": metadata
            })

        except Exception as e:
            print(f"✗ {item['name']}: Error - {e}")
            failed.append(item)

    # Create manifest
    manifest_path = output_path / "MANIFEST.md"
    with open(manifest_path, 'w') as f:
        f.write("# Test Dataset - Known Devices with GPS\n\n")
        f.write("This dataset contains validated RF captures from Flipper Zero unit tests\n")
        f.write("with added GPS coordinates for testing RTL_433 integration.\n\n")
        f.write("## Files\n\n")

        for item in successful:
            f.write(f"### {item['filename']}\n\n")
            f.write(f"- **Device:** {item['name']}\n")
            f.write(f"- **Protocol:** {item['metadata'].protocol}\n")
            f.write(f"- **Frequency:** {item['metadata'].frequency / 1_000_000:.3f} MHz\n")
            f.write(f"- **Format:** {item['metadata'].file_format}\n")
            if item['metadata'].has_raw_data:
                f.write(f"- **Pulses:** {item['metadata'].pulse_count}\n")
            f.write(f"- **GPS:** {item['gps']['lat']}, {item['gps']['lon']}\n")
            f.write(f"- **Description:** {item['description']}\n")
            f.write(f"- **Source:** {item['source']}\n\n")

    # Summary
    print("="*70)
    print("SUMMARY")
    print("="*70)
    print(f"Successfully created: {len(successful)} files")
    print(f"Failed: {len(failed)} files")
    print(f"Output directory: {output_path}")
    print(f"Manifest: {manifest_path}")
    print()

    if successful:
        print("Test these files with:")
        print(f"  PYTHONPATH=. python3 scripts/test_rtl433_with_known_devices.py")

    return successful, failed


if __name__ == '__main__':
    create_test_dataset()
