"use client";

import AddAgencyPackPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddAgencyPackPlan";
import AddAiModeTrackerPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddAiModeTrackerPlan";
import AddAiOverviewsTrackerPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddAiOverviewsTrackerPlan";
import AddAiSearchToolkitPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddAiSearchToolkitPlan";
import AddAiWriterPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddAiWriterPlan";
import AddBacklinkCheckerPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddBacklinkCheckerPlan";
import AddChatgptTrackerPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddChatgptTrackerPlan";
import AddCompetitorAnalysisToolPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddCompetitorAnalysisToolPlan";
import AddContentEditorPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddContentEditorPlan";
import AddContentMarketingToolPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddContentMarketingToolPlan";
import AddGeminiTrackerPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddGeminiTrackerPlan";
import AddInsightsPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddInsightsPlan";
import AddKeywordGrouperPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddKeywordGrouperPlan";
import AddKeywordToolPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddKeywordToolPlan";
import AddLeadGeneratorPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddLeadGeneratorPlan";
import AddLocalMarketingSoftwarePlan from "@/components/Dashboard/Admin/ServicePlanManager/AddLocalMarketingSoftwarePlan";
import AddOnPageSeoCheckerPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddOnPageSeoCheckerPlan";
import AddRankTrackerPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddRankTrackerPlan";
import AddSeoDashboardPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddSeoDashboardPlan";
import AddSeoReportGeneratorPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddSeoReportGeneratorPlan";
import AddSerpTrackerPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddSerpTrackerPlan";
import AddWebsiteAuditPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddWebsiteAuditPlan";
import AddWhiteLabelPlan from "@/components/Dashboard/Admin/ServicePlanManager/AddWhiteLabelPlan";
import EditAgencyPackPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditAgencyPackPlan";
import EditAiModeTrackerPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditAiModeTrackerPlan";
import EditAiOverviewsTrackerPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditAiOverviewsTrackerPlan";
import EditAiSearchToolkitPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditAiSearchToolkitPlan";
import EditAiWriterPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditAiWriterPlan";
import EditBacklinkCheckerPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditBacklinkCheckerPlan";
import EditChatgptTrackerPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditChatgptTrackerPlan";
import EditCompetitorAnalysisToolPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditCompetitorAnalysisToolPlan";
import EditContentEditorPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditContentEditorPlan";
import EditContentMarketingToolPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditContentMarketingToolPlan";
import EditGeminiTrackerPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditGeminiTrackerPlan";
import EditInsightsPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditInsightsPlan";
import EditKeywordGrouperPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditKeywordGrouperPlan";
import EditKeywordToolPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditKeywordToolPlan";
import EditLeadGeneratorPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditLeadGeneratorPlan";
import EditLocalMarketingSoftwarePlan from "@/components/Dashboard/Admin/ServicePlanManager/EditLocalMarketingSoftwarePlan";
import EditOnPageSeoCheckerPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditOnPageSeoCheckerPlan";
import EditRankTrackerPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditRankTrackerPlan";
import EditSeoDashboardPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditSeoDashboardPlan";
import EditSeoReportGeneratorPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditSeoReportGeneratorPlan";
import EditSerpTrackerPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditSerpTrackerPlan";
import EditWebsiteAuditPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditWebsiteAuditPlan";
import EditWhiteLabelPlan from "@/components/Dashboard/Admin/ServicePlanManager/EditWhiteLabelPlan";
import { Edit, Plus } from "lucide-react";
import { useState } from "react";

const services = [
  { key: "rank-tracker", label: "Rank Tracker" },
  { key: "on-page-seo-checker", label: "On-Page SEO Checker" },
  { key: "website-audit", label: "Website Audit" },
  {
    key: "competitor-analysis-tool",
    label: "Competitor Analysis Tool",
  },
  { key: "backlink-checker", label: "Backlink Checker" },
  { key: "local-marketing-software", label: "Local Marketing Software" },
  { key: "ai-search-toolkit", label: "AI Search Toolkit" },
  { key: "ai-overviews-tracker", label: "AI Overviews Tracker" },
  { key: "ai-mode-tracker", label: "AI Mode Tracker" },
  { key: "chatgpt-tracker", label: "ChatGPT Tracker" },
  { key: "gemini-tracker", label: "Gemini Tracker" },
  { key: "keyword-tool", label: "Keyword Tool" },
  { key: "content-marketing-tool", label: "Content Marketing Tool" },
  { key: "content-editor", label: "Content Editor" },
  { key: "ai-writer", label: "AI Writer" },
  { key: "serp-tracker", label: "SERP Tracker" },
  { key: "insights", label: "SEO Insights" },
  { key: "keyword-grouper", label: "Keyword Grouper" },
  { key: "seo-dashboard", label: "SEO Dashboard" },
  { key: "agency-pack", label: "Agency Pack" },
  { key: "seo-report-generator", label: "SEO Report Generator" },
  { key: "lead-generator", label: "Lead Generator" },
  { key: "white-label", label: "White Label" },
];

export default function PlanManager() {
  const [selectedService, setSelectedService] = useState("");
  const [showModal, setShowModal] = useState(false);
  const [mode, setMode] = useState("add");

  const handleOpen = () => {
    setShowModal(true);
  };

  const handleClose = () => {
    setShowModal(false);
  };

  const renderComponent = () => {
    if (!selectedService) return null;

    if (!showModal) {
      return (
        <div className="flex justify-center mt-6">
          <button
            onClick={handleOpen}
            className="flex items-center gap-2 px-6 py-3 rounded-xl text-sm font-medium text-white bg-[#794AFF] shadow-sm hover:shadow-md hover:opacity-90 transition-all"
          >
            {mode === "add" ? (
              <>
                <Plus size={16} />
                Add New Plan
              </>
            ) : (
              <>
                <Edit size={16} />
                Edit Plan
              </>
            )}
          </button>
        </div>
      );
    }

    if (selectedService === "rank-tracker") {
      return mode === "add" ? (
        <AddRankTrackerPlan onClose={handleClose} />
      ) : (
        <EditRankTrackerPlan onClose={handleClose} />
      );
    }

    if (selectedService === "on-page-seo-checker") {
      return mode === "add" ? (
        <AddOnPageSeoCheckerPlan onClose={handleClose} />
      ) : (
        <EditOnPageSeoCheckerPlan onClose={handleClose} />
      );
    }

    if (selectedService === "website-audit") {
      return mode === "add" ? (
        <AddWebsiteAuditPlan onClose={handleClose} />
      ) : (
        <EditWebsiteAuditPlan onClose={handleClose} />
      );
    }

    if (selectedService === "competitor-analysis-tool") {
      return mode === "add" ? (
        <AddCompetitorAnalysisToolPlan onClose={handleClose} />
      ) : (
        <EditCompetitorAnalysisToolPlan onClose={handleClose} />
      );
    }

    if (selectedService === "backlink-checker") {
      return mode === "add" ? (
        <AddBacklinkCheckerPlan onClose={handleClose} />
      ) : (
        <EditBacklinkCheckerPlan onClose={handleClose} />
      );
    }

    if (selectedService === "local-marketing-software") {
      return mode === "add" ? (
        <AddLocalMarketingSoftwarePlan onClose={handleClose} />
      ) : (
        <EditLocalMarketingSoftwarePlan onClose={handleClose} />
      );
    }

    if (selectedService === "ai-search-toolkit") {
      return mode === "add" ? (
        <AddAiSearchToolkitPlan onClose={handleClose} />
      ) : (
        <EditAiSearchToolkitPlan onClose={handleClose} />
      );
    }

    if (selectedService === "ai-overviews-tracker") {
      return mode === "add" ? (
        <AddAiOverviewsTrackerPlan onClose={handleClose} />
      ) : (
        <EditAiOverviewsTrackerPlan onClose={handleClose} />
      );
    }

    if (selectedService === "ai-mode-tracker") {
      return mode === "add" ? (
        <AddAiModeTrackerPlan onClose={handleClose} />
      ) : (
        <EditAiModeTrackerPlan onClose={handleClose} />
      );
    }

    if (selectedService === "chatgpt-tracker") {
      return mode === "add" ? (
        <AddChatgptTrackerPlan onClose={handleClose} />
      ) : (
        <EditChatgptTrackerPlan onClose={handleClose} />
      );
    }

    if (selectedService === "gemini-tracker") {
      return mode === "add" ? (
        <AddGeminiTrackerPlan onClose={handleClose} />
      ) : (
        <EditGeminiTrackerPlan onClose={handleClose} />
      );
    }

    if (selectedService === "keyword-tool") {
      return mode === "add" ? (
        <AddKeywordToolPlan onClose={handleClose} />
      ) : (
        <EditKeywordToolPlan onClose={handleClose} />
      );
    }

    if (selectedService === "content-marketing-tool") {
      return mode === "add" ? (
        <AddContentMarketingToolPlan onClose={handleClose} />
      ) : (
        <EditContentMarketingToolPlan onClose={handleClose} />
      );
    }

    if (selectedService === "content-editor") {
      return mode === "add" ? (
        <AddContentEditorPlan onClose={handleClose} />
      ) : (
        <EditContentEditorPlan onClose={handleClose} />
      );
    }

    if (selectedService === "ai-writer") {
      return mode === "add" ? (
        <AddAiWriterPlan onClose={handleClose} />
      ) : (
        <EditAiWriterPlan onClose={handleClose} />
      );
    }

    if (selectedService === "serp-tracker") {
      return mode === "add" ? (
        <AddSerpTrackerPlan onClose={handleClose} />
      ) : (
        <EditSerpTrackerPlan onClose={handleClose} />
      );
    }

    if (selectedService === "insights") {
      return mode === "add" ? (
        <AddInsightsPlan onClose={handleClose} />
      ) : (
        <EditInsightsPlan onClose={handleClose} />
      );
    }

    if (selectedService === "keyword-grouper") {
      return mode === "add" ? (
        <AddKeywordGrouperPlan onClose={handleClose} />
      ) : (
        <EditKeywordGrouperPlan onClose={handleClose} />
      );
    }

    if (selectedService === "seo-dashboard") {
      return mode === "add" ? (
        <AddSeoDashboardPlan onClose={handleClose} />
      ) : (
        <EditSeoDashboardPlan onClose={handleClose} />
      );
    }

    if (selectedService === "agency-pack") {
      return mode === "add" ? (
        <AddAgencyPackPlan onClose={handleClose} />
      ) : (
        <EditAgencyPackPlan onClose={handleClose} />
      );
    }

    if (selectedService === "seo-report-generator") {
      return mode === "add" ? (
        <AddSeoReportGeneratorPlan onClose={handleClose} />
      ) : (
        <EditSeoReportGeneratorPlan onClose={handleClose} />
      );
    }

    if (selectedService === "lead-generator") {
      return mode === "add" ? (
        <AddLeadGeneratorPlan onClose={handleClose} />
      ) : (
        <EditLeadGeneratorPlan onClose={handleClose} />
      );
    }

    if (selectedService === "white-label") {
      return mode === "add" ? (
        <AddWhiteLabelPlan onClose={handleClose} />
      ) : (
        <EditWhiteLabelPlan onClose={handleClose} />
      );
    }
  };

  return (
    <div className="space-y-6">
      <div>
        <h2 className="text-lg lg:text-xl font-semibold">Plan Manager</h2>
        <p className="text-sm lg:text-base text-[#6A7282]">
          Manage plans and pricing
        </p>
      </div>

      <div className="bg-[#1A2035] border border-[#FFFFFF14] rounded-2xl p-5">
        <div className="grid grid-cols-1 md:grid-cols-2 gap-6">
          <div>
            <label className="text-xs lg:text-sm text-[#6A7282]">
              Select Service
            </label>

            <select
              value={selectedService}
              onChange={(e) => setSelectedService(e.target.value)}
              className="mt-1 w-full px-4 py-2.5 rounded-xl bg-[#FFFFFF12] border border-white/10 text-sm text-white outline-none focus:ring-2 focus:ring-[#4F46E5] [&>option]:bg-[#0F172A] [&>option]:text-white"
            >
              <option value="">Select Service</option>

              {services.map((s) => (
                <option key={s.key} value={s.key}>
                  {s.label}
                </option>
              ))}
            </select>
          </div>

          <div>
            <label className="text-xs lg:text-sm text-[#6A7282]">Action</label>

            <select
              value={mode}
              onChange={(e) => setMode(e.target.value)}
              className="mt-1 w-full px-4 py-2.5 rounded-xl bg-[#FFFFFF12] border border-white/10 text-sm text-white outline-none focus:ring-2 focus:ring-[#4F46E5] [&>option]:bg-[#0F172A] [&>option]:text-white"
            >
              <option value="add">Add Plan</option>
              <option value="edit">Edit Plan</option>
            </select>
          </div>
        </div>
      </div>

      {selectedService && <div className="mt-6">{renderComponent()}</div>}

      {!selectedService && (
        <div className="mt-10 text-center text-[#676767] text-sm">
          Select a service and action to manage plans
        </div>
      )}
    </div>
  );
}
