import re
from pathlib import Path

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

# Wrap sheet form in stepper
text = text.replace(
    """          <div className="flex-1 overflow-y-auto px-6 py-5 flex flex-col gap-5">

            {/* 1. Основные */}
            <div className="flex flex-col gap-4">
              <SectionTitle>Основные</SectionTitle>""",
    """          <Stepper value={sheetStep} onValueChange={setSheetStep} className="flex-1 flex flex-col min-h-0 px-6 py-5">
            <StepperNav className="mb-5">
              <StepperItem step={1}>
                <StepperTrigger>
                  <StepperIndicator>1</StepperIndicator>
                  <StepperTitle className="sr-only">Основные</StepperTitle>
                </StepperTrigger>
                <StepperSeparator />
              </StepperItem>
              <StepperItem step={2}>
                <StepperTrigger>
                  <StepperIndicator>2</StepperIndicator>
                  <StepperTitle className="sr-only">WAN</StepperTitle>
                </StepperTrigger>
                <StepperSeparator />
              </StepperItem>
              <StepperItem step={3}>
                <StepperTrigger>
                  <StepperIndicator>3</StepperIndicator>
                  <StepperTitle className="sr-only">API</StepperTitle>
                </StepperTrigger>
                <StepperSeparator />
              </StepperItem>
              <StepperItem step={4}>
                <StepperTrigger>
                  <StepperIndicator>4</StepperIndicator>
                  <StepperTitle className="sr-only">Дополнительно</StepperTitle>
                </StepperTrigger>
              </StepperItem>
            </StepperNav>
            <StepperPanel className="flex-1 overflow-y-auto">
              <StepperContent value={1} className="flex flex-col gap-4">""",
    1,
)

text = text.replace(
    """            </div>

            {/* 2. WAN-аплинки (только для home-router) */}
            {isHomeRouter && (
              <div className="flex flex-col gap-4">
                <SectionTitle>WAN-аплинки</SectionTitle>""",
    """              </StepperContent>
              <StepperContent value={2} className="flex flex-col gap-4">
                <SectionTitle>WAN-аплинки</SectionTitle>""",
    1,
)

text = text.replace(
    """              </div>
            )}

            {/* 3. Подключение (API) */}
            <div className="flex flex-col gap-4">
              <SectionTitle>Подключение (RouterOS REST API)</SectionTitle>""",
    """              </StepperContent>
              <StepperContent value={3} className="flex flex-col gap-4">
              <SectionTitle>Подключение (RouterOS REST API)</SectionTitle>""",
    1,
)

text = text.replace(
    """            </div>

            {/* 4. Дополнительно */}
            <div className="flex flex-col gap-4">
              <button type="button" onClick={() => set("showAdvanced", !form.showAdvanced)}""",
    """              </StepperContent>
              <StepperContent value={4} className="flex flex-col gap-4">
              <button type="button" onClick={() => set("showAdvanced", !form.showAdvanced)}""",
    1,
)

text = text.replace(
    """            </div>

          </div>

          <SheetFooter className="px-6 py-4 border-t shrink-0 flex-row gap-2">
            <SheetClose render={<Button variant="outline" className="flex-1" />}>Отмена</SheetClose>
            <Button className="flex-1" onClick={handleSave}>
              {sheetMode === "edit" ? "Сохранить" : "Добавить сервер"}
            </Button>
          </SheetFooter>""",
    """              </StepperContent>
            </StepperPanel>
          </Stepper>

          <SheetFooter className="px-6 py-4 border-t shrink-0 flex-row gap-2">
            <SheetClose render={<Button variant="outline" />}>Отмена</SheetClose>
            {sheetStep > 1 && (
              <Button variant="outline" onClick={() => setSheetStep((s) => s - 1)}>Назад</Button>
            )}
            {sheetStep < 4 ? (
              <Button className="ml-auto" onClick={() => setSheetStep((s) => s + 1)}>Далее</Button>
            ) : (
              <Button className="ml-auto" onClick={handleSave}>
                {sheetMode === "edit" ? "Сохранить" : "Добавить сервер"}
              </Button>
            )}
          </SheetFooter>""",
    1,
)

# WAN step 2: show message when not home router
text = text.replace(
    """              <StepperContent value={2} className="flex flex-col gap-4">
                <SectionTitle>WAN-аплинки</SectionTitle>
                <WanUplinkEditor""",
    """              <StepperContent value={2} className="flex flex-col gap-4">
                <SectionTitle>WAN-аплинки</SectionTitle>
                {!isHomeRouter ? (
                  <p className="text-sm text-muted-foreground">WAN-аплинки доступны только для типа Home Router.</p>
                ) : (
                <WanUplinkEditor""",
    1,
)

text = text.replace(
    """                />
              </StepperContent>
              <StepperContent value={3}""",
    """                />
                )}
              </StepperContent>
              <StepperContent value={3}""",
    1,
)

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