import { useState } from 'react';
import type { Meta, StoryObj } from '@storybook/react';
import { PackagesStep } from './PackagesStep';
import type { PackageData, PackagesStepProvider } from './PackagesStep';
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' },
      { label: 'Log in', href: '/login' },
    ]}
  />
);

const mockProvider: PackagesStepProvider = {
  name: 'H.Parsons Funeral Directors',
  location: 'Wentworth, NSW',
  imageUrl: 'https://placehold.co/120x80/E8E0D6/8B6F47?text=H.Parsons',
  rating: 4.6,
  reviewCount: 7,
};

const matchedPackages: PackageData[] = [
  {
    id: 'everyday',
    name: 'Everyday Funeral Package',
    price: 2700,
    description:
      'This package includes a funeral service at a chapel or a church with a funeral procession. It includes many of the most commonly selected funeral options.',
    sections: [
      {
        heading: 'Essentials',
        items: [
          { name: 'Accommodation', price: 500 },
          { name: 'Death registration certificate', price: 150 },
          { name: 'Doctor fee for Cremation', price: 150 },
          { name: 'NSW Government Levy - Cremation', price: 83 },
          { name: 'Professional Mortuary Care', price: 1200 },
          { name: 'Professional Service Fee', price: 1120 },
        ],
      },
      {
        heading: 'Complimentary Items',
        items: [
          { name: 'Dressing Fee', price: 0 },
          { name: 'Viewing Fee', price: 0 },
        ],
      },
    ],
    total: 2700,
    extras: {
      heading: 'Extras',
      items: [
        { name: 'Allowance for Flowers', price: 150, isAllowance: true },
        { name: 'Allowance for Master of Ceremonies', price: 500, isAllowance: true },
        { name: 'After Business Hours Service Surcharge', price: 150 },
        { name: 'After Hours Prayers', price: 1920 },
        { name: 'Coffin Bearing by Funeral Directors', price: 1500 },
        { name: 'Digital Recording', price: 500 },
      ],
    },
    terms:
      'This package includes a funeral service at a chapel or a church with a funeral procession. Pricing may vary based on additional selections.',
  },
  {
    id: 'essential',
    name: 'Essential Funeral Package',
    price: 1800,
    description:
      'A simple, dignified option covering the essential requirements for a cremation service.',
    sections: [
      {
        heading: 'Essentials',
        items: [
          { name: 'Death registration certificate', price: 150 },
          { name: 'Professional Mortuary Care', price: 800 },
          { name: 'Professional Service Fee', price: 850 },
        ],
      },
    ],
    total: 1800,
  },
];

const otherPackages: PackageData[] = [
  {
    id: 'deluxe',
    name: 'Deluxe Funeral Package',
    price: 4900,
    description:
      'A comprehensive package with premium inclusions, higher-quality coffin selection, and expanded service options for families wanting a more personalised farewell.',
    sections: [
      {
        heading: 'Essentials',
        items: [
          { name: 'Accommodation', price: 750 },
          { name: 'Death registration certificate', price: 150 },
          { name: 'Professional Mortuary Care', price: 1500 },
          { name: 'Professional Service Fee', price: 1500 },
          { name: 'Premium Coffin', price: 1000 },
        ],
      },
    ],
    total: 4900,
  },
  {
    id: 'catholic',
    name: 'Catholic Service',
    price: 3200,
    description:
      'Tailored for Catholic funeral traditions including a Requiem Mass, graveside prayers, and coordination with parish requirements.',
    sections: [
      {
        heading: 'Essentials',
        items: [
          { name: 'Accommodation', price: 500 },
          { name: 'Professional Mortuary Care', price: 1200 },
          { name: 'Professional Service Fee', price: 1200 },
          { name: 'Church coordination', price: 300 },
        ],
      },
    ],
    total: 3200,
  },
];

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

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

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

// ─── Interactive (default) ──────────────────────────────────────────────────

/** Matched + other packages — select a package, see detail, click Make Arrangement */
export const Default: Story = {
  render: () => {
    const [selectedId, setSelectedId] = useState<string | null>(null);

    return (
      <PackagesStep
        provider={mockProvider}
        packages={matchedPackages}
        otherPackages={otherPackages}
        selectedPackageId={selectedId}
        onSelectPackage={setSelectedId}
        onArrange={() => alert('Open ArrangementDialog')}
        onProviderClick={() => alert('Open provider profile')}
        onBack={() => alert('Back')}
        navigation={nav}
      />
    );
  },
};

// ─── With selection ─────────────────────────────────────────────────────────

/** Package already selected — detail panel visible */
export const WithSelection: Story = {
  render: () => {
    const [selectedId, setSelectedId] = useState<string | null>('everyday');

    return (
      <PackagesStep
        provider={mockProvider}
        packages={matchedPackages}
        otherPackages={otherPackages}
        selectedPackageId={selectedId}
        onSelectPackage={setSelectedId}
        onArrange={() => alert('Open ArrangementDialog')}
        onProviderClick={() => alert('Open provider profile')}
        onBack={() => alert('Back')}
        navigation={nav}
      />
    );
  },
};

// ─── No other packages (all match) ─────────────────────────────────────────

/** All packages match filters — no "Other packages" section */
export const AllMatching: Story = {
  render: () => {
    const [selectedId, setSelectedId] = useState<string | null>(null);

    return (
      <PackagesStep
        provider={mockProvider}
        packages={[...matchedPackages, ...otherPackages]}
        selectedPackageId={selectedId}
        onSelectPackage={setSelectedId}
        onArrange={() => alert('Open ArrangementDialog')}
        onProviderClick={() => alert('Open provider profile')}
        onBack={() => alert('Back')}
        navigation={nav}
      />
    );
  },
};

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

/** Pre-planning flow — softer copy */
export const PrePlanning: Story = {
  render: () => {
    const [selectedId, setSelectedId] = useState<string | null>(null);

    return (
      <PackagesStep
        provider={mockProvider}
        packages={matchedPackages}
        otherPackages={otherPackages}
        selectedPackageId={selectedId}
        onSelectPackage={setSelectedId}
        onArrange={() => alert('Open ArrangementDialog')}
        onProviderClick={() => alert('Open provider profile')}
        onBack={() => alert('Back')}
        navigation={nav}
        isPrePlanning
      />
    );
  },
};

// ─── Validation error ───────────────────────────────────────────────────────

/** Error shown when no package selected */
export const WithError: Story = {
  render: () => {
    const [selectedId, setSelectedId] = useState<string | null>(null);

    return (
      <PackagesStep
        provider={mockProvider}
        packages={matchedPackages}
        selectedPackageId={selectedId}
        onSelectPackage={setSelectedId}
        onArrange={() => {}}
        onBack={() => alert('Back')}
        error="Please choose a package to continue."
        navigation={nav}
      />
    );
  },
};
