import { useState } from 'react';
import { useNavigate, useParams } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import {
  Alert,
  Badge,
  Button,
  Divider,
  Group,
  Paper,
  Stack,
  Stepper,
  Text,
  ThemeIcon,
  Title,
} from '@mantine/core';
import {
  IconAlertTriangle,
  IconArrowLeft,
  IconCircleCheck,
  IconClock,
  IconDownload,
  IconInfoCircle,
  IconShip,
} from '@tabler/icons-react';
import { notify } from '@ema-platform/ui';
import { useDateDisplayer } from '@ema-platform/shared';
import { MOCK_REGISTRATIONS, recordDownload, STATUS_COLOR } from '../mock';

// ponytail: placeholder PDF blob; wire real cert endpoint when backend lands.
const BLANK_PDF = 'data:application/pdf;base64,JVBERi0xLjQKJcfsj6IKMSAwIG9iago8PC9UeXBlL0NhdGFsb2cvUGFnZXMgMiAwIFI+PgplbmRvYmoKMiAwIG9iago8PC9UeXBlL1BhZ2VzL0tpZHNbMyAwIFJdL0NvdW50IDE+PgplbmRvYmoKMyAwIG9iago8PC9UeXBlL1BhZ2UvUGFyZW50IDIgMCBSL01lZGlhQm94WzAgMCA2MTIgNzkyXT4+CmVuZG9iagp4cmVmCjAgNAowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDAwMDkgMDAwMDAgbiAKMDAwMDAwMDA1OCAwMDAwMCBuIAowMDAwMDAwMTE1IDAwMDAwIG4gCnRyYWlsZXIKPDwvU2l6ZSA0L1Jvb3QgMSAwIFI+PgpzdGFydHhyZWYKMjE3CiUlRU9G';

function downloadCertificate(filename: string) {
  const a = document.createElement('a');
  a.href = BLANK_PDF;
  a.download = filename;
  a.click();
}

export function VesselRegistrationStatusPage() {
  const { t } = useTranslation();
  const navigate = useNavigate();
  const showDate = useDateDisplayer();
  const { id } = useParams();
  const [reg] = useState(() => MOCK_REGISTRATIONS.find((r) => r.id === id) ?? null);
  const [, forceUpdate] = useState(0);

  if (!reg) {
    return (
      <Stack gap="md">
        <Button variant="subtle" leftSection={<IconArrowLeft size={14} />} onClick={() => navigate('/vessel-registrations')}>{t('common.back')}</Button>
        <Alert variant="light" color="red" icon={<IconInfoCircle size={15} />}>{t('vesselRegistration.status.notFound')}</Alert>
      </Stack>
    );
  }

  const activeStep = reg.timeline.filter((step) => step.done).length - 1;
  const needsCorrection = reg.status === 'Correction Required' || reg.status === 'Rejected';

  const handleDownload = (certName: string, certNumber: string) => {
    downloadCertificate(`${certName.replace(/\s+/g, '-')}-${certNumber}.pdf`);
    recordDownload(reg.id, certName);
    forceUpdate((n) => n + 1);
    notify.success(t('vesselRegistration.status.downloadedNotify', { certName }));
  };

  const renewalKey = reg.renewal === 'Overdue'
    ? (reg.expiryDate ? 'vesselRegistration.status.renewalOverdueWithExpiry' : 'vesselRegistration.status.renewalOverdue')
    : (reg.expiryDate ? 'vesselRegistration.status.renewalDueSoonWithExpiry' : 'vesselRegistration.status.renewalDueSoon');

  return (
    <Stack gap="md">
      <Group gap="sm">
        <Button variant="subtle" leftSection={<IconArrowLeft size={14} />} onClick={() => navigate('/vessel-registrations')}>{t('common.back')}</Button>
        <div>
          <Title order={3}>{reg.vesselName}</Title>
          <Text fz="sm" c="dimmed">{reg.id} — {reg.category}</Text>
        </div>
      </Group>

      <Paper withBorder radius="lg" p="xl">
        <Group justify="space-between" mb="md" wrap="wrap" gap="sm">
          <Group gap="sm">
            <ThemeIcon size={36} radius="md" color="blue" variant="light"><IconShip size={18} /></ThemeIcon>
            <div>
              <Text fw={700} fz="sm">{t('vesselRegistration.status.title')}</Text>
              <Text fz="xs" c="dimmed">{t('vesselRegistration.status.submitted', { date: reg.submitted })}</Text>
            </div>
          </Group>
          <Badge color={STATUS_COLOR[reg.status]} variant="light" size="lg">{reg.status}</Badge>
        </Group>

        {reg.renewal && reg.renewal !== 'OK' && (
          <Alert
            variant="light"
            color={reg.renewal === 'Overdue' ? 'red' : 'orange'}
            icon={<IconAlertTriangle size={15} />}
            mb="md"
            p="sm"
          >
            <Text fz="sm">
              {t(renewalKey, { date: reg.expiryDate ? showDate(reg.expiryDate) : undefined })}
            </Text>
          </Alert>
        )}

        {reg.remarks && (
          <Alert variant="light" color={needsCorrection ? 'orange' : 'blue'} icon={<IconInfoCircle size={15} />} mb="md" p="sm">
            <Text fz="sm" fw={600} mb={2}>{t('vesselRegistration.status.officerRemarks')}</Text>
            <Text fz="sm">{reg.remarks}</Text>
          </Alert>
        )}

        <Stepper active={activeStep} size="sm" color="teal">
          {reg.timeline.map((step, i) => (
            <Stepper.Step
              key={i}
              label={step.event}
              description={step.date ?? t('vesselRegistration.status.pending')}
              icon={step.done ? <IconCircleCheck size={16} /> : <IconClock size={16} />}
            />
          ))}
        </Stepper>

        {needsCorrection && (
          <Group justify="flex-end" mt="md">
            <Button color="orange" onClick={() => navigate('/vessel-registrations/apply')}>{t('vesselRegistration.status.resubmit')}</Button>
          </Group>
        )}
      </Paper>

      {reg.status === 'Approved' && reg.certificates && (
        <Paper withBorder radius="lg" p="xl">
          <Text fw={700} mb="md">{t('vesselRegistration.status.certificatesTitle')}</Text>
          <Stack gap="sm">
            {reg.certificates.map((cert) => (
              <div key={cert.name}>
                <Group justify="space-between" wrap="wrap" gap="sm">
                  <div>
                    <Text fw={600} fz="sm">{cert.name}</Text>
                    <Text fz="xs" c="dimmed">{t('vesselRegistration.status.certificateNumber', { number: cert.number, date: showDate(cert.issueDate) })}</Text>
                    {cert.downloads > 0 && (
                      <Text fz="xs" c="dimmed">{t('vesselRegistration.status.downloadedCount', { count: cert.downloads })}</Text>
                    )}
                  </div>
                  <Button
                    size="xs"
                    leftSection={<IconDownload size={14} />}
                    onClick={() => handleDownload(cert.name, cert.number)}
                  >
                    {t('common.download')}
                  </Button>
                </Group>
                <Divider mt="sm" />
              </div>
            ))}
          </Stack>
        </Paper>
      )}
    </Stack>
  );
}
