import {Profile} from "@/domain/models/Profile";
import {Experience} from "@/domain/models/Experience";
import {Project} from "@/domain/models/Project";
import {SkillGroup} from "@/domain/models/Skill";
import {Education} from "@/domain/models/Education";
import {Certification} from "@/domain/models/Certification";
import profileMockData from "@/infrastructure/mock/profile.json";
import experienceMockData from "@/infrastructure/mock/experience.json";
import educationMockData from "@/infrastructure/mock/education.json";
import certificationMockData from "@/infrastructure/mock/certification.json";
import skillMockData from "@/infrastructure/mock/skill.json";
import projectMockData from "@/infrastructure/mock/project.json";

const useMock = import.meta.env.VITE_USE_MOCK === "true";

export class ProfileService {

    async getProfile(): Promise<Profile> {
        if (useMock) return profileMockData.content;
        const res = await fetch(`${import.meta.env.VITE_API_URL}/profiles/${import.meta.env.VITE_PORTFOLIO_SLUG}`);
        if (!res.ok) console.log(new Error("Error fetching personal data"));
        return await res.json();
    }

    async getExperience(profileId: number): Promise<Experience[]> {
        if (useMock) return experienceMockData.content;
        const res = await fetch(`${import.meta.env.VITE_API_URL}/profiles/${profileId}/experience`);
        if (!res.ok) console.log(new Error("Error fetching experience data"));
        return await res.json();
    }

    async getProjects(profileId: number): Promise<Project[]> {
        if (useMock) return projectMockData.content;
        const res = await fetch(`${import.meta.env.VITE_API_URL}/profiles/${profileId}/projects`);
        if (!res.ok) console.log(new Error("Error fetching projects data"));
        return await res.json();
    }

    async getSkills(profileId: number): Promise<SkillGroup[]> {
        if (useMock) return skillMockData.content;
        const res = await fetch(`${import.meta.env.VITE_API_URL}/profiles/${profileId}/skills`);
        if (!res.ok) console.log(new Error("Error fetching skills data"));
        return await res.json();
    }

    async getEducation(profileId: number): Promise<Education[]> {
        if (useMock) return educationMockData.content;
        const res = await fetch(`${import.meta.env.VITE_API_URL}/profiles/${profileId}/education`);
        if (!res.ok) console.log(new Error("Error fetching education data"));
        return await res.json();
    }

    async getCertifications(profileId: number): Promise<Certification[]> {
        if (useMock) return certificationMockData.content;
        const res = await fetch(`${import.meta.env.VITE_API_URL}/profiles/${profileId}/certifications`);
        if (!res.ok) console.log(new Error("Error fetching certifications data"));
        return await res.json();
    }
}