/**
 * Main Puck configuration with all components categorized
 */
import type { Config } from '@measured/puck';
import type { ComponentProps } from './types';

// Layout components
import { Section } from './components/layout/Section';
import { Columns } from './components/layout/Columns';
import { Card } from './components/layout/Card';
import { Spacer } from './components/layout/Spacer';
import { Divider } from './components/layout/Divider';

// Content components
import { Heading } from './components/content/Heading';
import { RichText } from './components/content/RichText';
import { Image } from './components/content/Image';
import { Button } from './components/content/Button';
import { IconList } from './components/content/IconList';
import { Testimonial } from './components/content/Testimonial';

// Booking components
import { BookingWidget } from './components/booking/BookingWidget';
import { ServiceCatalog } from './components/booking/ServiceCatalog';
import { Services } from './components/booking/Services';

// Contact components
import { ContactForm } from './components/contact/ContactForm';
import { BusinessHours } from './components/contact/BusinessHours';
import { AddressBlock } from './components/contact/AddressBlock';
import { Map } from './components/contact/Map';

// Marketing components (Page Builder)
import { Header } from './components/marketing/Header';
import { Hero } from './components/marketing/Hero';
import { LogoCloud } from './components/marketing/LogoCloud';
import { SplitContent } from './components/marketing/SplitContent';
import { CTASection } from './components/marketing/CTASection';
import { StatsStrip } from './components/marketing/StatsStrip';
import { GalleryGrid } from './components/marketing/GalleryGrid';
import { Testimonials } from './components/marketing/Testimonials';
import { VideoEmbed } from './components/marketing/VideoEmbed';
import { ContentBlocks } from './components/marketing/ContentBlocks';
import { PricingCards } from './components/marketing/PricingCards';
import { Footer } from './components/marketing/Footer';
import { FAQAccordion } from './components/marketing/FAQAccordion';
import { FullBookingFlow } from './components/marketing/FullBookingFlow';

// Legacy components (for backward compatibility)
import { config as legacyConfig } from '../puckConfig';

// Component categories for the editor palette
export const componentCategories = {
  layout: {
    title: 'Layout',
    components: ['Section', 'Columns', 'Card', 'Spacer', 'Divider'],
  },
  navigation: {
    title: 'Navigation',
    components: ['Header', 'Footer'],
  },
  hero: {
    title: 'Hero',
    components: ['Hero'],
  },
  content: {
    title: 'Content',
    components: [
      'SplitContent',
      'ContentBlocks',
      'GalleryGrid',
      'Heading',
      'RichText',
      'Image',
      'Button',
      'IconList',
    ],
  },
  trust: {
    title: 'Trust & Social Proof',
    components: ['LogoCloud', 'Testimonials', 'StatsStrip', 'Testimonial'],
  },
  conversion: {
    title: 'Conversion',
    components: ['CTASection', 'PricingCards'],
  },
  media: {
    title: 'Media',
    components: ['VideoEmbed'],
  },
  info: {
    title: 'Information',
    components: ['FAQAccordion', 'FAQ'],
  },
  booking: {
    title: 'Booking',
    components: ['FullBookingFlow', 'BookingWidget', 'ServiceCatalog', 'Services'],
  },
  contact: {
    title: 'Contact',
    components: ['ContactForm', 'BusinessHours', 'AddressBlock', 'Map'],
  },
  legacy: {
    title: 'Legacy',
    components: ['TextSection', 'Booking'],
  },
};

// Full config with all components
export const puckConfig: Config<ComponentProps> = {
  categories: {
    layout: {
      title: 'Layout',
      components: ['Section', 'Columns', 'Card', 'Spacer', 'Divider'],
    },
    navigation: {
      title: 'Navigation',
      components: ['Header', 'Footer'],
    },
    hero: {
      title: 'Hero',
      components: ['Hero'],
    },
    content: {
      title: 'Content',
      components: ['SplitContent', 'ContentBlocks', 'GalleryGrid', 'Heading', 'RichText', 'Image', 'Button', 'IconList'],
    },
    trust: {
      title: 'Trust & Social Proof',
      components: ['LogoCloud', 'Testimonials', 'StatsStrip', 'Testimonial'],
    },
    conversion: {
      title: 'Conversion',
      components: ['CTASection', 'PricingCards'],
    },
    media: {
      title: 'Media',
      components: ['VideoEmbed'],
    },
    info: {
      title: 'Information',
      components: ['FAQAccordion'],
    },
    booking: {
      title: 'Booking',
      components: ['FullBookingFlow', 'BookingWidget', 'ServiceCatalog', 'Services'],
    },
    contact: {
      title: 'Contact',
      components: ['ContactForm', 'BusinessHours', 'AddressBlock', 'Map'],
    },
    legacy: {
      title: 'Legacy',
      components: ['TextSection', 'Booking'],
      defaultExpanded: false,
    },
  },
  components: {
    // Navigation components
    Header: {
      ...Header,
    },
    Footer: {
      ...Footer,
    },

    // Hero components
    Hero: {
      ...Hero,
    },

    // Content components
    SplitContent: {
      ...SplitContent,
    },
    ContentBlocks: {
      ...ContentBlocks,
    },
    GalleryGrid: {
      ...GalleryGrid,
    },
    Heading: {
      ...Heading,
    },
    RichText: {
      ...RichText,
    },
    Image: {
      ...Image,
    },
    Button: {
      ...Button,
    },
    IconList: {
      ...IconList,
    },

    // Trust & Social Proof components
    LogoCloud: {
      ...LogoCloud,
    },
    Testimonials: {
      ...Testimonials,
    },
    StatsStrip: {
      ...StatsStrip,
    },
    Testimonial: {
      ...Testimonial,
    },

    // Conversion components
    CTASection: {
      ...CTASection,
    },
    PricingCards: {
      ...PricingCards,
    },

    // Media components
    VideoEmbed: {
      ...VideoEmbed,
    },

    // Information components
    FAQAccordion: {
      ...FAQAccordion,
    },

    // Booking components
    FullBookingFlow: {
      ...FullBookingFlow,
    },
    BookingWidget: {
      ...BookingWidget,
    },
    ServiceCatalog: {
      ...ServiceCatalog,
    },
    Services: {
      ...Services,
    },

    // Contact components
    ContactForm: {
      ...ContactForm,
    },
    BusinessHours: {
      ...BusinessHours,
    },
    AddressBlock: {
      ...AddressBlock,
    },
    Map: {
      ...Map,
    },

    // Layout components
    Section: {
      ...Section,
    },
    Columns: {
      ...Columns,
    },
    Card: {
      ...Card,
    },
    Spacer: {
      ...Spacer,
    },
    Divider: {
      ...Divider,
    },

    // Legacy components (for backward compatibility)
    TextSection: {
      ...legacyConfig.components.TextSection,
    },
    Booking: {
      ...legacyConfig.components.Booking,
    },
  },
};

// Render-only config (includes all components, no gating)
export const renderConfig = puckConfig;

// Editor config factory (can exclude components based on features)
export function getEditorConfig(features?: {
  can_use_service_catalog?: boolean;
}): Config<ComponentProps> {
  // Deep clone the config to avoid mutating the original
  const config = {
    ...puckConfig,
    components: { ...puckConfig.components },
    categories: puckConfig.categories ? JSON.parse(JSON.stringify(puckConfig.categories)) : undefined,
  };

  // Remove gated components if features not available
  if (features?.can_use_service_catalog === false) {
    delete config.components.ServiceCatalog;
    // Also remove from categories
    if (config.categories?.booking) {
      config.categories.booking.components = config.categories.booking.components.filter(
        (c: string) => c !== 'ServiceCatalog'
      );
    }
  }

  return config;
}

export default puckConfig;
