//
//  Exercise.swift
//  Werkout_ios
//
//  Created by Trey Tartt on 6/14/23.
//

import Foundation

struct ExerciseElement: Codable {
    let workout: Int
    let exercise: ExerciseExercise
    let weight: Int?
    let reps: Int?
    let duration: Int?
    let durationAudio: String?
    let weightAudio: String?
    let createdAt: String
    
    enum CodingKeys: String, CodingKey {
        case workout, exercise, weight, reps, duration
        case durationAudio = "duration_audio"
        case weightAudio = "weight_audio"
        case createdAt = "created_at"
    }
    
    var createdAtDate: Date {
        let df = DateFormatter()
        df.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        df.locale = Locale(identifier: "en_US_POSIX")
        return df.date(from: self.createdAt) ?? Date()
    }
}

struct ExerciseExercise: Codable, Hashable {
    static func == (lhs: ExerciseExercise, rhs: ExerciseExercise) -> Bool {
        lhs.id == rhs.id
    }
    
    let id: Int
    let muscles: [ExerciseMuscle]
    let equipment: [ExerciseEquipment]
    let audioURL, videoURL, createdAt, updatedAt: String
    let name, description, side: String
    let isTwoDumbbells, isTrackableDistance, isAlternating, isWeight: Bool
    let isDistance, isDuration, isReps: Bool
    let jointsUsed, movementPatterns, equipmentRequired, muscleGroups: String
    let synonyms: String?
    
    enum CodingKeys: String, CodingKey {
        case id, muscles, equipment
        case audioURL = "audio_url"
        case videoURL = "video_url"
        case createdAt = "created_at"
        case updatedAt = "updated_at"
        case name, description, side
        case isTwoDumbbells = "is_two_dumbbells"
        case isTrackableDistance = "is_trackable_distance"
        case isAlternating = "is_alternating"
        case isWeight = "is_weight"
        case isDistance = "is_distance"
        case isDuration = "is_duration"
        case isReps = "is_reps"
        case jointsUsed = "joints_used"
        case movementPatterns = "movement_patterns"
        case equipmentRequired = "equipment_required"
        case muscleGroups = "muscle_groups"
        case synonyms
    }
    
    var createdAtDate: Date {
        let df = DateFormatter()
        df.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSZ"
        df.locale = Locale(identifier: "en_US_POSIX")
        return df.date(from: self.createdAt) ?? Date()
    }
}
