import { Alert, Button, Group, Modal, Stack, Text } from "@mantine/core";
import type { UseFormReturnType } from "@mantine/form";
import type { PromoCodeFormValues } from "@/lib/features/promo-codes/types";
import { PromoCodeForm } from "./PromoCodeForm";
interface CreatePromoCodeModalProps {
  opened: boolean;
  onClose: () => void;
  form: UseFormReturnType<PromoCodeFormValues>;
  onSubmit: (values: PromoCodeFormValues) => void;
  busy: boolean;
  // Set when the form was pre-filled from an existing promo code (super-admin
  // duplicate). Holds the source code, purely for the banner.
  duplicateOf?: string | null;
}
export const CreatePromoCodeModal: React.FC<CreatePromoCodeModalProps> = ({
  opened,
  onClose,
  form,
  onSubmit,
  busy,
  duplicateOf,
}) => {
  return (
    <Modal
      opened={opened}
      onClose={onClose}
      title={duplicateOf ? "Duplicate Promo Code" : "Create Promo Code"}
      size="xl"
    >
      <form onSubmit={form.onSubmit(onSubmit)}>
        <Stack>
          {duplicateOf ? (
            <Alert color="grape" variant="light">
              <Text size="sm">
                Pre-filled from <b>{duplicateOf}</b>. The code must be unique —
                change it (and anything else) before creating.
              </Text>
            </Alert>
          ) : null}
          <PromoCodeForm form={form} />
          <Group justify="flex-end" mt="md">
            <Button variant="default" onClick={onClose}>
              Cancel
            </Button>
            <Button type="submit" loading={busy}>
              Create
            </Button>
          </Group>
          {form.errors._form ? (
            <Alert color="red" variant="light">
              <Text size="sm">{form.errors._form}</Text>
            </Alert>
          ) : null}
        </Stack>
      </form>
    </Modal>
  );
};
