//
// ExerciseListLoader.swift
// Workouts
//
// Copyright 2025 Rouslan Zenetl. All Rights Reserved.
//

import Foundation
import Yams

class ExerciseListLoader {
    struct ExerciseListData: Codable {
        let name: String
        let source: String
        let exercises: [ExerciseItem]

        struct ExerciseItem: Codable, Identifiable {
            let name: String
            let descr: String
            let type: String
            let split: String

            var id: String { name }
        }
    }

    static func loadExerciseLists() -> [String: ExerciseListData] {
        var exerciseLists: [String: ExerciseListData] = [:]

        guard let resourcePath = Bundle.main.resourcePath else {
            print("Could not find resource path")
            return exerciseLists
        }

        do {
            let fileManager = FileManager.default
            let resourceURL = URL(fileURLWithPath: resourcePath)
            let yamlFiles = try fileManager.contentsOfDirectory(at: resourceURL, includingPropertiesForKeys: nil)
                .filter { $0.pathExtension == "yaml" && $0.lastPathComponent.hasSuffix(".exercises.yaml") }

            for yamlFile in yamlFiles {
                let fileName = yamlFile.lastPathComponent
                do {
                    let yamlString = try String(contentsOf: yamlFile, encoding: .utf8)
                    if let exerciseList = try Yams.load(yaml: yamlString) as? [String: Any],
                       let name = exerciseList["name"] as? String,
                       let source = exerciseList["source"] as? String,
                       let exercisesData = exerciseList["exercises"] as? [[String: Any]] {

                        var exercises: [ExerciseListData.ExerciseItem] = []

                        for exerciseData in exercisesData {
                            if let name = exerciseData["name"] as? String,
                               let descr = exerciseData["descr"] as? String,
                               let type = exerciseData["type"] as? String,
                               let split = exerciseData["split"] as? String {
                                let exercise = ExerciseListData.ExerciseItem(name: name, descr: descr, type: type, split: split)
                                exercises.append(exercise)
                            }
                        }

                        let exerciseList = ExerciseListData(name: name, source: source, exercises: exercises)
                        exerciseLists[fileName] = exerciseList
                    }
                } catch {
                    print("Error loading YAML file \(fileName): \(error)")
                }
            }
        } catch {
            print("Error listing directory contents: \(error)")
        }

        return exerciseLists
    }
}
