import re
from pathlib import Path

path = Path(__file__).resolve().parents[2] / "app" / "(main)" / "servers" / "page.tsx"
text = path.read_text(encoding="utf-8")

text = text.replace(
    'import { Fragment, useEffect, useMemo, useState } from "react"\n'
    'import { PageHeader } from "@/components/page-header"\n'
    'import { StatusBadge } from "@/components/status-badge"',
    'import { useEffect, useMemo, useState } from "react"\n'
    'import { PageHeader } from "@/components/page-header"\n'
    'import { FormField, FormToggle, SectionTitle, SegmentedControl } from "@/components/form-kit"\n'
    'import { DataPageToolbar } from "@/components/data-page-toolbar"\n'
    'import { ServersDataGrid } from "@/components/data-grids/servers-data-grid"',
    1,
)

text = text.replace(
    """import {
  DropdownMenu, DropdownMenuTrigger, DropdownMenuContent,
  DropdownMenuItem, DropdownMenuSeparator, DropdownMenuLabel, DropdownMenuGroup,
} from "@/components/ui/dropdown-menu"
""",
    """import {
  Stepper,
  StepperContent,
  StepperIndicator,
  StepperItem,
  StepperNav,
  StepperPanel,
  StepperSeparator,
  StepperTitle,
  StepperTrigger,
} from "@/components/reui/stepper"
""",
    1,
)

text = text.replace(
    """import {
  SearchIcon, RefreshCwIcon, DownloadIcon, PlusIcon, TrashIcon,
  MoreHorizontalIcon, EyeIcon, EyeOffIcon,
  ChevronRightIcon, ChevronDownIcon,
  CheckCircleIcon, XCircleIcon, LoaderCircleIcon,
  ShieldIcon, WifiIcon, PencilIcon, PowerIcon, Trash2Icon, ExternalLinkIcon,
  HomeIcon, ServerIcon, NetworkIcon,
} from "lucide-react\"""",
    """import {
  RefreshCwIcon, DownloadIcon, PlusIcon, TrashIcon,
  EyeIcon, EyeOffIcon,
  ChevronRightIcon, ChevronDownIcon,
  CheckCircleIcon, XCircleIcon, LoaderCircleIcon,
  ShieldIcon, WifiIcon,
  HomeIcon, ServerIcon, NetworkIcon,
} from "lucide-react\"""",
    1,
)

text = re.sub(
    r"// ─── RouterOS version utilities.*?// ─── Countries ─",
    "// ─── Countries ─",
    text,
    count=1,
    flags=re.S,
)

text = re.sub(
    r"// ─── Type config ─.*?// ─── Shared small components ─",
    "// ─── Shared small components ─",
    text,
    count=1,
    flags=re.S,
)

text = re.sub(
    r"function Field\(\{ label, hint, required, children \}:.*?^}\n\n// ─── Country field",
    "// ─── Country field",
    text,
    count=1,
    flags=re.S | re.M,
)

text = text.replace(
    "const [expandedId,   setExpandedId]   = useState<string | null>(null)",
    "const [sheetStep,    setSheetStep]    = useState(1)",
    1,
)

text = text.replace(
    'setForm(defaultForm); setTestState("idle"); setTestMsg("")\n    setOpen(true)',
    'setForm(defaultForm); setTestState("idle"); setTestMsg("")\n    setSheetStep(1)\n    setOpen(true)',
    1,
)

text = text.replace(
    'setTestState("idle"); setTestMsg(""); setOpen(true)',
    'setTestState("idle"); setTestMsg(""); setSheetStep(1); setOpen(true)',
    1,
)

text = re.sub(r"<Field\b", "<FormField", text)
text = re.sub(r"</Field>", "</FormField>", text)
text = re.sub(r"<Toggle\b", "<FormToggle", text)

new_table = """          {/* Table */}
          <Card>
            <DataPageToolbar
              segmented={{
                value: typeFilter,
                onChange: setTypeFilter,
                options: tabs.map((tab) => ({
                  value: tab.value,
                  label: tab.label,
                  count: tab.value === "all" ? counts.all : counts[tab.value as ServerType],
                })),
              }}
              search={search}
              onSearchChange={setSearch}
              searchPlaceholder="Поиск по имени, хосту…"
              countLabel={`${filtered.length} серверов`}
            />
            <ServersDataGrid
              servers={filtered}
              isLive={isLive}
              pollingIds={pollingIds}
              onPoll={handlePoll}
              onEdit={openEdit}
              onDelete={handleDelete}
              onToggleStatus={handleToggleStatus}
            />
          </Card>
"""

text = re.sub(
    r"          \{/\* Table \*/\}\n          <Card>.*?</Card>\n",
    new_table,
    text,
    count=1,
    flags=re.S,
)

path.write_text(text, encoding="utf-8")
print("patched", path)
