"use client";
import React, { useState } from "react";
import {
  Box,
  Button,
  Typography,
  Paper,
  Container,
  useTheme,
  useMediaQuery,
} from "@mui/material";
import CheckCircleIcon from "@mui/icons-material/CheckCircle";
import IdentityForm from "./forms/identity/IdentityForm";
import PersonalInfoForm from "./forms/PersonalInfoForm";
import PhysicalInfoForm from "./forms/PhysicalInfoForm";
import EducationSection from "./forms/EducationSection";
import JobRequestSection from "./forms/JobRequestSection";
import CourseSection from "./forms/CourseSection";
import SkillsForm from "./forms/SkillsForm";
import { WorkExperienceSection } from "./forms/WorkExperienceSection";
import JobInfoForm from "./forms/JobInfoForm";
import { ReferralSection } from "./forms/ReferralForm";
import RelationsForm from "./forms/RelationForm";
import RegistrationCenterForm from "./forms/register-center/RegistrationCenterForm";
import { INITIAL_WIZARD_DATA, WizardFormData } from "@/core/types";

// کامپوننت پیش‌فرض برای مراحلی که هنوز نساختید
const PlaceholderStep = ({ step }: any) => (
  <Typography color="text.secondary">
    محتوای مرحله {step} در حال طراحی است...
  </Typography>
);

// --- ۲. نگاشت (Mapping) مراحل به کامپوننت‌ها ---

const STEP_COMPONENTS: Record<number, React.ComponentType<any>> = {
  1: RegistrationCenterForm,
  2: IdentityForm,
  3: PersonalInfoForm,
  4: PhysicalInfoForm,
  5: EducationSection,
  6: JobRequestSection,
  7: CourseSection,
  8: SkillsForm,
  9: WorkExperienceSection,
  10: JobInfoForm,
  11: ReferralSection,
  12: RelationsForm,
  // بقیه مراحل از Placeholder استفاده می‌کنند
};

const STEP_LABELS = [
  "انتخاب مركز",
  "مشخصات هويتي",
  "مشخصات فردي",
  "مشخصات ظاهري",
  "مشخصات تحصيلي",
  "شغل درخواستي",
  "دوره هاي آموزشي",
  "مهارت ها",
  "سوابق كاري",
  "اطلاعات كاري",
  "معرف",
  "مشخصات آشنايان",
];

// --- ۳. کامپوننت اصلی استپر ---

export default function MultiStepForm() {
  const [activeStep, setActiveStep] = useState(1);
  const [maxStepReached, setMaxStepReached] = useState(1);
  const [formData, setFormData] = useState<WizardFormData>(INITIAL_WIZARD_DATA);

  const updateFormData = (patch: Partial<WizardFormData>) => {
    setFormData((prev) => ({ ...prev, ...patch }));
  };

  const theme = useTheme();
  const isMobile = useMediaQuery(theme.breakpoints.down("md"));

  const ActiveStepComponent = STEP_COMPONENTS[activeStep] || PlaceholderStep;

  return (
    <Box
      sx={{
        minHeight: "100vh",
        display: "flex",
        alignItems: "center",
        bgcolor: "#f8fafc",
        py: 4,
      }}
    >
      <Container maxWidth="xl">
        <div
          style={{
            display: "flex",
            flexDirection: isMobile ? "column" : "row",
            gap: "40px",
          }}
        >
          {/* Sidebar Navigation */}
          {!isMobile && (
            <div style={{ width: "200px", flexShrink: 0 }}>
              <Typography
                variant="h5"
                sx={{ fontWeight: 900, mb: 4, color: "#1e293b" }}
              >
                پنل ثبت مرکز
              </Typography>
              {STEP_LABELS.map((label, i) => (
                <Box
                  key={i}
                  onClick={() =>
                    i + 1 <= maxStepReached && setActiveStep(i + 1)
                  }
                  sx={{
                    display: "flex",
                    alignItems: "center",
                    mb: 2,
                    cursor: i + 1 <= maxStepReached ? "pointer" : "not-allowed",
                    opacity: i + 1 <= maxStepReached ? 1 : 0.4,
                    color: i + 1 === activeStep ? "#2563eb" : "#64748b",
                    transition: "0.2s",
                  }}
                >
                  <Box
                    sx={{
                      width: 32,
                      height: 32,
                      borderRadius: "10px",
                      mr: 2,
                      display: "flex",
                      alignItems: "center",
                      justifyContent: "center",
                      bgcolor:
                        i + 1 === activeStep
                          ? "#2563eb"
                          : i + 1 < activeStep
                            ? "#4CAF5033"
                            : "#f1f5f9",
                      color: i + 1 === activeStep ? "#fff" : "#2563eb",
                      fontWeight: 700,
                      fontSize: "0.8rem",
                      border:
                        i + 1 === activeStep ? "none" : "1px solid #e2e8f0",
                    }}
                  >
                    {i + 1 < activeStep ? (
                      <CheckCircleIcon sx={{ fontSize: 18 }} color="success" />
                    ) : (
                      Number(i + 1).toLocaleString("fa-IR")
                    )}
                  </Box>
                  <Typography
                    variant="body2"
                    sx={{
                      fontWeight: i + 1 === activeStep ? 800 : 500,
                      color: i + 1 < activeStep ? "green" : "",
                    }}
                  >
                    {label}
                  </Typography>
                </Box>
              ))}
            </div>
          )}

          {/* Main Form Area */}
          <div style={{ flexGrow: 1 }}>
            <Paper
              sx={{
                p: { xs: 3, md: 6 },
                borderRadius: "35px",
                boxShadow: "0 25px 50px -12px rgba(0,0,0,0.05)",
                border: "1px solid #e2e8f0",
              }}
            >
              <Box sx={{ mb: 4 }}>
                <Typography
                  variant="overline"
                  sx={{ color: "#2563eb", fontWeight: 900 }}
                >
                  مرحله {activeStep.toLocaleString("fa-IR")} از{" "}
                  {STEP_LABELS.length.toLocaleString("fa-IR")}
                </Typography>
                <Typography
                  variant="h4"
                  sx={{ fontWeight: 900, color: "#0f172a" }}
                >
                  {STEP_LABELS[activeStep - 1]}
                </Typography>
              </Box>

              {/* رندر شدن داینامیک کامپوننت مرحله فعلی */}
              <div className="w-full">
                <ActiveStepComponent
                  data={formData} // کل دیتای فرم
                  update={updateFormData} // تابع آپدیت‌کننده
                  step={activeStep}
                  setStep={setActiveStep}
                />
              </div>
            </Paper>
          </div>
        </div>
      </Container>
    </Box>
  );
}
