import { HeroSection } from "@/components/sections/portfolio/HeroSection";
import { AboutSection } from "@/components/sections/portfolio/AboutSection";
import { TechStackSection } from "@/components/sections/portfolio/TechStackSection";
import { ProjectsSection } from "@/components/sections/portfolio/ProjectsSection";
import { ExperienceSection } from "@/components/sections/portfolio/ExperienceSection";
import { PrincipleSection } from "@/components/sections/portfolio/PrincipleSection";
import { ContactSection } from "@/components/sections/portfolio/ContactSection";
import { prisma } from "@/lib/prisma";

const fallbackProjects = [
  {
    id: "1",
    title: "Enterprise ERP System",
    description:
      "Full-scale ERP system built with NestJS microservices, handling inventory, finance, and HR modules for 500+ concurrent users.",
    imageUrl: null,
    liveUrl: null,
    githubUrl: "https://github.com",
    techStack: ["NestJS", "Next.js", "PostgreSQL", "Redis", "Docker"],
    featured: true,
    order: 1,
    createdAt: new Date(),
    updatedAt: new Date(),
  },
  {
    id: "2",
    title: "Real-time Analytics Dashboard",
    description:
      "Interactive analytics platform with real-time data visualization, WebSocket integration, and multi-tenant architecture.",
    imageUrl: null,
    liveUrl: null,
    githubUrl: "https://github.com",
    techStack: ["Next.js", "NestJS", "Prisma", "Chart.js", "PostgreSQL"],
    featured: true,
    order: 2,
    createdAt: new Date(),
    updatedAt: new Date(),
  },
  {
    id: "3",
    title: "API Gateway & Auth Service",
    description:
      "Scalable API Gateway with JWT authentication, rate limiting, and microservice orchestration using Clean Architecture principles.",
    imageUrl: null,
    liveUrl: null,
    githubUrl: "https://github.com",
    techStack: ["NestJS", "Redis", "JWT", "Docker", "Kubernetes"],
    featured: true,
    order: 3,
    createdAt: new Date(),
    updatedAt: new Date(),
  },
];

const fallbackExperiences = [
  {
    id: "1",
    company: "Tech Corp Indonesia",
    role: "Senior Fullstack Engineer",
    startDate: new Date("2022-01-01"),
    endDate: null,
    current: true,
    description:
      "Leading development of enterprise-grade web applications with focus on scalability and clean architecture.",
    highlights: [
      "Architected microservices-based ERP system serving 500+ concurrent users",
      "Reduced API response time by 60% through Redis caching & query optimization",
      "Mentored 3 junior developers on NestJS patterns and SOLID principles",
    ],
    techStack: ["Next.js", "NestJS", "PostgreSQL", "Redis", "Docker"],
    order: 1,
    createdAt: new Date(),
    updatedAt: new Date(),
  },
  {
    id: "2",
    company: "Startup Fintech",
    role: "Fullstack Developer",
    startDate: new Date("2020-03-01"),
    endDate: new Date("2021-12-31"),
    current: false,
    description:
      "Built core financial services platform from ground up with emphasis on security and performance.",
    highlights: [
      "Developed payment processing system handling Rp 50B+ monthly transactions",
      "Built real-time notification system using WebSockets and Redis pub/sub",
      "Implemented automated testing achieving 85%+ code coverage",
    ],
    techStack: ["React", "Node.js", "Express", "MongoDB", "PostgreSQL"],
    order: 2,
    createdAt: new Date(),
    updatedAt: new Date(),
  },
];

async function getProjects() {
  try {
    return await prisma.project.findMany({
      where: { featured: true },
      orderBy: { order: "asc" },
    });
  } catch {
    return fallbackProjects;
  }
}

async function getExperiences() {
  try {
    return await prisma.experience.findMany({
      orderBy: { order: "asc" },
    });
  } catch {
    return fallbackExperiences;
  }
}

export default async function HomePage() {
  const [projects, experiences] = await Promise.all([
    getProjects(),
    getExperiences(),
  ]);

  return (
    <>
      <HeroSection />
      <AboutSection />
      <TechStackSection />
      <ProjectsSection projects={projects} />
      <ExperienceSection experiences={experiences} />
      <PrincipleSection />
      <ContactSection />
    </>
  );
}
