package app.padelclub.lestorage

import kotlinx.cinterop.ExperimentalForeignApi
import platform.Foundation.NSFileManager
import platform.Foundation.NSString
import platform.Foundation.NSUTF8StringEncoding
import platform.Foundation.create
import platform.Foundation.writeToFile

class IosFileWriter : FileWriter {
    @OptIn(ExperimentalForeignApi::class)
    override fun writeFile(directory: String, filename: String, content: String) {
        val fileManager = NSFileManager.defaultManager

        // Create directory if it doesn't exist
        fileManager.createDirectoryAtPath(
            directory,
            withIntermediateDirectories = true,
            attributes = null,
            error = null
        )

        // Write file
        val filePath = "$directory/$filename"
        val nsString = NSString.create(string = content)
        nsString.writeToFile(
            filePath,
            atomically = true,
            encoding = NSUTF8StringEncoding,
            error = null
        )
    }
}
