import Foundation

struct Track: Identifiable, Codable {
    let id: UUID
    let name: String
    private let filePath: String
    let duration: TimeInterval
    
    var url: URL {
        return URL(fileURLWithPath: filePath)
    }
    
    init(name: String, url: URL, duration: TimeInterval) {
        self.id = UUID()
        self.name = name
        self.filePath = url.path
        self.duration = duration
    }
    
    enum CodingKeys: String, CodingKey {
        case id, name, filePath, duration
    }
    
    init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        id = try container.decode(UUID.self, forKey: .id)
        name = try container.decode(String.self, forKey: .name)
        filePath = try container.decode(String.self, forKey: .filePath)
        duration = try container.decode(TimeInterval.self, forKey: .duration)
    }
    
    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)
        try container.encode(id, forKey: .id)
        try container.encode(name, forKey: .name)
        try container.encode(filePath, forKey: .filePath)
        try container.encode(duration, forKey: .duration)
    }
}
