//
//  UITestSampleData.swift
//  Cable
//  Created by Stefan Lange-Hegermann on 06.10.25.

import Foundation
import SwiftData

enum UITestSampleData {
    static let argument = "--uitest-sample-data"

    static func prepareIfNeeded(container: ModelContainer) {
#if DEBUG
        guard ProcessInfo.processInfo.arguments.contains(argument) else { return }

        let context = ModelContext(container)

        do {
            try clearExistingData(in: context)
            try seedSampleData(in: context)
            try context.save()
        } catch {
            assertionFailure("Failed to seed UI test sample data: \(error)")
        }
#endif
    }
}

#if DEBUG
private extension UITestSampleData {
    static func clearExistingData(in context: ModelContext) throws {
        let systemDescriptor = FetchDescriptor<ElectricalSystem>()
        let loadDescriptor = FetchDescriptor<SavedLoad>()
        let itemDescriptor = FetchDescriptor<Item>()

        let systems = try context.fetch(systemDescriptor)
        let loads = try context.fetch(loadDescriptor)
        let items = try context.fetch(itemDescriptor)

        systems.forEach { context.delete($0) }
        loads.forEach { context.delete($0) }
        items.forEach { context.delete($0) }
    }

    static func seedSampleData(in context: ModelContext) throws {
        let adventureVan = ElectricalSystem(
            name: String(localized: "sample.system.rv.name", comment: "Sample data name for the adventure van system"),
            location: String(localized: "sample.system.rv.location", comment: "Sample data location for the adventure van system"),
            iconName: "bus",
            colorName: "orange"
        )
        adventureVan.timestamp = Date(timeIntervalSinceReferenceDate: 3000)

        let workshopBench = ElectricalSystem(
            name: String(localized: "sample.system.workshop.name", comment: "Sample data name for the workshop system"),
            location: String(localized: "sample.system.workshop.location", comment: "Sample data location for the workshop system"),
            iconName: "wrench.adjustable",
            colorName: "teal"
        )
        workshopBench.timestamp = Date(timeIntervalSinceReferenceDate: 2000)

        context.insert(adventureVan)
        context.insert(workshopBench)

        let vanFridge = SavedLoad(
            name: String(localized: "sample.load.fridge.name", comment: "Sample data load name for a compressor fridge"),
            voltage: 12.0,
            current: 4.2,
            power: 50.0,
            length: 6.0,
            crossSection: 6.0,
            iconName: "snowflake",
            colorName: "blue",
            isWattMode: true,
            system: adventureVan,
            identifier: "sample.load.fridge"
        )
        vanFridge.timestamp = Date(timeIntervalSinceReferenceDate: 1100)

        let vanLighting = SavedLoad(
            name: String(localized: "sample.load.lighting.name", comment: "Sample data load name for LED strip lighting"),
            voltage: 12.0,
            current: 2.0,
            power: 24.0,
            length: 10.0,
            crossSection: 2.5,
            iconName: "lightbulb",
            colorName: "yellow",
            isWattMode: false,
            system: adventureVan,
            identifier: "sample.load.lighting"
        )
        vanLighting.timestamp = Date(timeIntervalSinceReferenceDate: 1200)

        let workshopCompressor = SavedLoad(
            name: String(localized: "sample.load.compressor.name", comment: "Sample data load name for an air compressor"),
            voltage: 120.0,
            current: 8.0,
            power: 960.0,
            length: 15.0,
            crossSection: 16.0,
            iconName: "hammer",
            colorName: "red",
            isWattMode: true,
            system: workshopBench,
            identifier: "sample.load.compressor"
        )
        workshopCompressor.timestamp = Date(timeIntervalSinceReferenceDate: 2100)

        let workshopCharger = SavedLoad(
            name: String(localized: "sample.load.charger.name", comment: "Sample data load name for a tool charger"),
            voltage: 120.0,
            current: 3.5,
            power: 420.0,
            length: 8.0,
            crossSection: 10.0,
            iconName: "battery.100",
            colorName: "green",
            isWattMode: false,
            system: workshopBench,
            identifier: "sample.load.charger"
        )
        workshopCharger.timestamp = Date(timeIntervalSinceReferenceDate: 2200)

        [vanFridge, vanLighting, workshopCompressor, workshopCharger].forEach { context.insert($0) }
    }
}
#endif
