//
//  ArchitectureGenerator.swift
//  
//
//  Created by Thibaut Schmitt on 18/11/2022.
//

import Foundation
import ToolCore

enum ArchitectureGenerator {

    static func writeArchitecture(_ architecture: ConfigurationArchitecture, projectDirectory: String) {
        // Create extension content
        var architectureContent = [
            "// Generated by ResgenSwift.\(Generate.toolName) \(ResgenSwiftVersion)",
            architecture.getClass()
        ]
            .joined(separator: "\n\n")

        architectureContent += "\n"

        let filename = "\(architecture.classname).swift"
        guard let filePath = architecture.path?.prependIfRelativePath(projectDirectory) else {
            let error = GenerateError.writeFile(filename, "Path of file is not defined.")
            print(error.description)
            Generate.exit(withError: error)
        }

        // Write content
        let architectureFilePathURL = URL(fileURLWithPath: "\(filePath)/\(filename)")
        do {
            try architectureContent.write(to: architectureFilePathURL, atomically: false, encoding: .utf8)
        } catch {
            let error = GenerateError.writeFile(filename, error.localizedDescription)
            print(error.description)
            Generate.exit(withError: error)
        }
    }
}
