import { useState } from 'react';
import type { Meta, StoryObj } from '@storybook/react';
import { CemeteryStep } from './CemeteryStep';
import type { CemeteryStepValues } from './CemeteryStep';
import { Navigation } from '../../organisms/Navigation';
import Box from '@mui/material/Box';

// ─── Helpers ─────────────────────────────────────────────────────────────────

const FALogo = () => (
  <Box sx={{ display: 'flex', alignItems: 'center' }}>
    <Box
      component="img"
      src="/brandlogo/logo-full.svg"
      alt="Funeral Arranger"
      sx={{ height: 28, display: { xs: 'none', md: 'block' } }}
    />
    <Box
      component="img"
      src="/brandlogo/logo-short.svg"
      alt="Funeral Arranger"
      sx={{ height: 28, display: { xs: 'block', md: 'none' } }}
    />
  </Box>
);

const nav = (
  <Navigation
    logo={<FALogo />}
    items={[
      { label: 'FAQ', href: '/faq' },
      { label: 'Contact Us', href: '/contact' },
    ]}
  />
);

const defaultValues: CemeteryStepValues = {
  ownPlot: null,
  hasPreference: null,
  cemeterySearch: '',
};

// ─── Meta ────────────────────────────────────────────────────────────────────

const meta: Meta<typeof CemeteryStep> = {
  title: 'Pages/CemeteryStep',
  component: CemeteryStep,
  tags: ['autodocs'],
  parameters: {
    layout: 'fullscreen',
  },
};

export default meta;
type Story = StoryObj<typeof CemeteryStep>;

// ─── Default ─────────────────────────────────────────────────────────────────

/** Initial state — no selections made */
export const Default: Story = {
  render: () => {
    const [values, setValues] = useState<CemeteryStepValues>({ ...defaultValues });
    return (
      <CemeteryStep
        values={values}
        onChange={setValues}
        onContinue={() => alert('Continue')}
        onBack={() => alert('Back')}
        onSaveAndExit={() => alert('Save')}
        navigation={nav}
      />
    );
  },
};

// ─── Owns a plot ─────────────────────────────────────────────────────────────

/** User owns a plot — shows "tell us where" + dropdown */
export const OwnsPlot: Story = {
  render: () => {
    const [values, setValues] = useState<CemeteryStepValues>({
      ...defaultValues,
      ownPlot: 'yes',
    });
    return (
      <CemeteryStep
        values={values}
        onChange={setValues}
        onContinue={() => alert('Continue')}
        onBack={() => alert('Back')}
        onSaveAndExit={() => alert('Save')}
        navigation={nav}
      />
    );
  },
};

// ─── No plot, has preference ─────────────────────────────────────────────────

/** No plot, has a preference — shows both questions + dropdown */
export const HasPreference: Story = {
  render: () => {
    const [values, setValues] = useState<CemeteryStepValues>({
      ...defaultValues,
      ownPlot: 'no',
      hasPreference: 'yes',
    });
    return (
      <CemeteryStep
        values={values}
        onChange={setValues}
        onContinue={() => alert('Continue')}
        onBack={() => alert('Back')}
        onSaveAndExit={() => alert('Save')}
        navigation={nav}
      />
    );
  },
};

// ─── No plot, no preference ──────────────────────────────────────────────────

/** No plot, no preference — provider will arrange */
export const NoPreference: Story = {
  render: () => {
    const [values, setValues] = useState<CemeteryStepValues>({
      ...defaultValues,
      ownPlot: 'no',
      hasPreference: 'no',
    });
    return (
      <CemeteryStep
        values={values}
        onChange={setValues}
        onContinue={() => alert('Continue')}
        onBack={() => alert('Back')}
        onSaveAndExit={() => alert('Save')}
        navigation={nav}
      />
    );
  },
};

// ─── Pre-planning ────────────────────────────────────────────────────────────

/** Pre-planning variant */
export const PrePlanning: Story = {
  render: () => {
    const [values, setValues] = useState<CemeteryStepValues>({ ...defaultValues });
    return (
      <CemeteryStep
        values={values}
        onChange={setValues}
        onContinue={() => alert('Continue')}
        onBack={() => alert('Back')}
        isPrePlanning
        navigation={nav}
      />
    );
  },
};
