As an expert developer and tutor could you rate these files' code and then provide these files after improvement? // localStorage.js

import { validate } from 'uuid';
import { Project } from './project.js';
import { Todo } from './todo.js';

/**
 * Manages saving and loading projects to and from localStorage.
 */
export class StorageManager {
    /**
     * Save projects to localStorage.
     * @param {Project[]} projects – Array of Project instances.
     */
    static saveProjects(projects) {
        try {
            const serializedProjects = JSON.stringify(projects);
            localStorage.setItem('projects', serializedProjects);
        } catch (error) {
            console.error(`Error saving to localStorage: ${error}`);
        }
    }

    /**
     * Loads projects from localStorage.
     * @returns {Project[]} Array of Project instances.
     */
    static loadProjects() {
        try {
            const serializedProjects = localStorage.getItem('projects');
            if (!serializedProjects) return [];

            const projectsData = JSON.parse(serializedProjects);

            return projectsData.map(validateAndConstructProject).filter((project) => project !== null);
        } catch (error) {
            console.error(`Failed to load projects from localStorage: ${error}`);
            // Optional: clean corrupted data
            localStorage.removeItem('projects');
            alert('Stored data was corrupted and has been reset.');
            // Return an empty array or default projects
            return [];
        }
    }
}

function validateAndConstructProject(projectData) {
    if (!projectData.name || typeof projectData.name !== 'string') {
        console.error(`Invalid project data: ${projectData}`);
        return null; // Skip invalid project
    }

    const project = new Project(projectData.name);

    if (Array.isArray(projectData.todos)) {
        project.todos = projectData.todos.map(validateAndConstructTodo).filter((todo) => todo !== null); // Remove invalid todos
    }

    return project;
}

function validateAndConstructTodo(todoData) {
    // Validate dueDate
    if (!todoData.title || !todoData.dueDate || isNaN(Date.parse(todoData.dueDate))) {
        console.error(`Invalid due date (or title) for todo: ${todoData.dueDate} in project: ${projectData.name}`);
        // todoData.dueDate = new Date().toISOString().split('T')[0]; // Assign current date if invalid
        return null; // Skip invalid todos
    }

    if (todoData.id && !validate(todoData.id)) {
        console.error(`Invalid UUID for todo: ${todoData.id}`);
        return null;
    }

    // Reconstruct Todo instance
    const todo = new Todo(
        todoData.title,
        todoData.dueDate,
        todoData.description,
        todoData.priority,
        todoData.tags,
        todoData.notes,
        todoData.id // Pass the existing ID
    );

    // Restore completion status
    todo.completed = todoData.completed ?? false;

    // // Reconstruct checklist if any
// if (Array.isArray(todoData.checklist)) {
//     todoData.checklist.forEach((item, index) => {
//         try {
//             todo.addChecklistItem(item.text);
//             todo.toggleChecklistItem(index); // Set completed status if necessary
//             if (item.completed) {
//                 todo.toggleChecklistItem(index);
//             }
//         } catch (chkError) {
//             console.error('Error reconstructing checklist item:', chkError);
//         }
//     });
// }

    // Reconstruct subtasks if any
    if (Array.isArray(todoData.subtasks)) {
        todo.subtasks = todoData.subtasks.map((subtaskData) => {
            const subtask = new Todo(
                subtaskData.title,
                subtaskData.dueDate,
                subtaskData.description,
                subtaskData.priority,
                subtaskData.tags,
                subtaskData.notes,
                subtaskData.id
            );
            subtask.completed = subtaskData.completed ?? false;
            return subtask;
        }).filter((subtask) => subtask !== null); // Remove invalid subtasks
    }

    // todo.recurrence = todoData.recurrence || null;
    return todo;
}