import Foundation

struct Employment: Identifiable {
    let employeeId: Int
    let businessId: Int
    let businessName: String
    let businessAddress: String
    let businessCity: String
    let employeeStatusId: Int
    let pendingTaskCount: Int
    let activeTaskCount: Int
    let roleId: Int  // 1=Staff, 2=Manager, 3=Admin

    var id: Int { employeeId }

    /// Decode directly from a [String: Any] dictionary (matches Flutter's fromJson)
    init(json: [String: Any]) {
        employeeId = WorkTask.parseInt(json["EmployeeID"]) ?? 0
        businessId = WorkTask.parseInt(json["BusinessID"]) ?? 0
        // Server returns "Name" not "BusinessName"
        businessName = (json["Name"] as? String) ?? (json["BusinessName"] as? String) ?? ""
        businessAddress = (json["Address"] as? String) ?? (json["BusinessAddress"] as? String) ?? ""
        businessCity = (json["City"] as? String) ?? (json["BusinessCity"] as? String) ?? ""
        // Match Flutter: read EmployeeStatusID first (server sends StatusID, which may differ)
        employeeStatusId = WorkTask.parseInt(json["EmployeeStatusID"] ?? json["StatusID"]) ?? 0
        pendingTaskCount = WorkTask.parseInt(json["PendingTaskCount"]) ?? 0
        activeTaskCount = WorkTask.parseInt(json["ActiveTaskCount"]) ?? 0
        roleId = WorkTask.parseInt(json["RoleID"] ?? json["ROLEID"] ?? json["roleId"] ?? json["role_id"]) ?? 1
    }

    var statusName: String {
        switch employeeStatusId {
        case 0: return "Pending"
        case 1: return "Active"
        case 2: return "Inactive"
        default: return "Unknown"
        }
    }

    /// Human-readable role name derived from roleId
    var roleName: String {
        switch roleId {
        case 1: return "Staff"
        case 2: return "Manager"
        case 3: return "Admin"
        default: return "Staff"
        }
    }
}
