"use client";

import { useRouter } from "next/navigation";
import {
  Button,
  Group,
  NumberInput,
  Stack,
  Switch,
  Text,
  Textarea,
  TextInput,
} from "@mantine/core";
import { useForm } from "@mantine/form";
import { notifications } from "@mantine/notifications";
import { DashboardPage } from "@/components/dashboard/DashboardPage";
import { PageHeader } from "@/components/dashboard/PageHeader";
import { PrimaryButton } from "@/components/ui/PrimaryButton";
import { adminAppCreationService } from "@/services/adminAppCreationService";
import { useAuthStore } from "@/store/authStore";

export default function NovoPlanoCriacaoPage() {
  const token = useAuthStore((s) => s.token);
  const router = useRouter();
  const form = useForm({
    initialValues: {
      name: "",
      slug: "",
      short_description: "",
      development_price: 0,
      featured: false,
      is_active: true,
    },
  });

  const handleSubmit = form.onSubmit(async (values) => {
    try {
      await adminAppCreationService.plans.create(token!, {
        name: values.name,
        slug: values.slug || undefined,
        short_description: values.short_description,
        development_price: values.development_price,
        monthly_price: 0,
        yearly_price: 0,
        featured: values.featured,
        is_active: values.is_active,
        ideal_for: [],
        platforms: ["pwa", "android"],
        features: [],
      });
      notifications.show({ color: "green", message: "Plano criado." });
      router.push("/dashboard/criacao-app/planos");
    } catch (e) {
      notifications.show({
        color: "red",
        message: e instanceof Error ? e.message : "Erro ao criar.",
      });
    }
  });

  return (
    <DashboardPage title="Novo plano">
      <PageHeader
        title="Novo plano de criação do app"
        description="Pagamento único do desenvolvimento. Hospedagem contrata-se em Planos."
      />
      <form onSubmit={handleSubmit}>
        <Stack maw={560} gap="md">
          <TextInput label="Nome" required {...form.getInputProps("name")} />
          <TextInput label="Slug" placeholder="starter" {...form.getInputProps("slug")} />
          <Textarea label="Descrição" {...form.getInputProps("short_description")} />
          <NumberInput
            label="Preço desenvolvimento (pagamento único)"
            min={0}
            decimalScale={2}
            thousandSeparator="."
            decimalSeparator=","
            required
            {...form.getInputProps("development_price")}
          />
          <Switch label="Destaque" {...form.getInputProps("featured", { type: "checkbox" })} />
          <Switch label="Ativo" {...form.getInputProps("is_active", { type: "checkbox" })} />
          <Group>
            <PrimaryButton type="submit">Salvar</PrimaryButton>
            <Button variant="subtle" onClick={() => router.back()}>
              Cancelar
            </Button>
          </Group>
        </Stack>
      </form>
    </DashboardPage>
  );
}
