import { logger } from '@/lib/logger';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { supabase } from '@/integrations/supabase/client';
import { useToast } from '@/hooks/use-toast';
import type { Json } from '@/integrations/supabase/types';


export interface GeneralSettings {
  homepageSlug: string;
  selectedTemplate?: string;
  contentReviewEnabled?: boolean;
}

const defaultGeneralSettings: GeneralSettings = {
  homepageSlug: 'home',
  contentReviewEnabled: false,
};

export interface SeoSettings {
  siteTitle: string;
  titleTemplate: string;
  defaultDescription: string;
  ogImage: string;
  twitterHandle: string;
  googleSiteVerification: string;
  robotsIndex: boolean;
  robotsFollow: boolean;
  developmentMode: boolean;
  requireAuthInDevMode: boolean;
}

export interface PerformanceSettings {
  lazyLoadImages: boolean;
  prefetchLinks: boolean;
  minifyHtml: boolean;
  enableServiceWorker: boolean;
  imageCacheMaxAge: number;
  cacheStaticAssets: boolean;
  // Edge caching
  enableEdgeCaching: boolean;
  edgeCacheTtlMinutes: number;
}

export interface BrandingSettings {
  // Identity
  logo?: string;
  logoDark?: string;
  favicon?: string;
  organizationName?: string;
  brandTagline?: string;
  
  // Admin panel branding
  adminName?: string;
  
  // Colors (HSL format)
  primaryColor?: string;
  secondaryColor?: string;
  accentColor?: string;
  
  // Typography
  headingFont?: string;
  bodyFont?: string;
  
  // Appearance
  borderRadius?: 'none' | 'sm' | 'md' | 'lg';
  shadowIntensity?: 'none' | 'subtle' | 'medium';
  heroOverlayOpacity?: 'none' | 'light' | 'medium' | 'strong';
  
  // Header display
  showLogoInHeader?: boolean;
  showNameWithLogo?: boolean;
  headerLogoSize?: 'sm' | 'md' | 'lg';
  
  // Theme toggle
  allowThemeToggle?: boolean;
  defaultTheme?: 'light' | 'dark' | 'system';
}

const defaultBrandingSettings: BrandingSettings = {
  logo: '',
  logoDark: '',
  favicon: '',
  organizationName: '',
  brandTagline: '',
  adminName: '', // Empty default - shows as "CMS" when not configured
  primaryColor: '220 100% 26%',
  secondaryColor: '210 40% 96%',
  accentColor: '199 89% 48%',
  headingFont: 'PT Serif',
  bodyFont: 'Inter',
  borderRadius: 'md',
  shadowIntensity: 'subtle',
  heroOverlayOpacity: 'medium',
  showLogoInHeader: true,
  showNameWithLogo: false,
  headerLogoSize: 'md',
  allowThemeToggle: true,
  defaultTheme: 'light',
};


const defaultSeoSettings: SeoSettings = {
  siteTitle: '',
  titleTemplate: '%s',
  defaultDescription: '',
  ogImage: '',
  twitterHandle: '',
  googleSiteVerification: '',
  robotsIndex: true,
  robotsFollow: true,
  developmentMode: false,
  requireAuthInDevMode: false,
};

const defaultPerformanceSettings: PerformanceSettings = {
  lazyLoadImages: true,
  prefetchLinks: true,
  minifyHtml: false,
  enableServiceWorker: false,
  imageCacheMaxAge: 31536000,
  cacheStaticAssets: true,
  enableEdgeCaching: false,
  edgeCacheTtlMinutes: 5,
};

export interface CustomScriptsSettings {
  headStart: string;
  headEnd: string;
  bodyStart: string;
  bodyEnd: string;
}

const defaultCustomScriptsSettings: CustomScriptsSettings = {
  headStart: '',
  headEnd: '',
  bodyStart: '',
  bodyEnd: '',
};

export interface CookieBannerSettings {
  enabled: boolean;
  title: string;
  description: string;
  policyLinkText: string;
  policyLinkUrl: string;
  acceptButtonText: string;
  rejectButtonText: string;
}

const defaultCookieBannerSettings: CookieBannerSettings = {
  enabled: true,
  title: 'We use cookies',
  description: 'We use cookies to improve your experience on our website, analyze traffic, and personalize content. By clicking "Accept all", you consent to our use of cookies.',
  policyLinkText: 'Read our Privacy Policy',
  policyLinkUrl: '/privacy-policy',
  acceptButtonText: 'Accept all',
  rejectButtonText: 'Essential only',
};

export interface MaintenanceSettings {
  enabled: boolean;
  title: string;
  message: string;
  expectedEndTime?: string;
}

const defaultMaintenanceSettings: MaintenanceSettings = {
  enabled: false,
  title: 'Site under maintenance',
  message: 'We are currently performing scheduled maintenance. The site will be back online shortly.',
  expectedEndTime: '',
};

// Chat settings
export type ChatAiProvider = 'openai' | 'gemini' | 'local' | 'n8n';
export type ChatSttProvider = 'browser' | 'openai' | 'gemini' | 'local';
export type ChatTtsProvider = 'none' | 'openai' | 'gemini' | 'local';
export type ChatWidgetStyle = 'floating' | 'pill' | 'expanded';
export type ChatWidgetSize = 'sm' | 'md' | 'lg';

export interface ChatSettings {
  // Core — `enabled` is deprecated; module on/off is the single source of truth
  /** @deprecated Use useIsModuleEnabled('chat') instead */
  enabled: boolean;
  title: string;
  placeholder: string;
  welcomeMessage: string;
  
  // AI-leverantör
  aiProvider: ChatAiProvider;
  
  // OpenAI (model selection only - keys managed via integrations)
  openaiModel: 'gpt-4o' | 'gpt-4o-mini' | 'gpt-3.5-turbo';
  openaiBaseUrl: string;
  
  // Google Gemini (model selection only - keys managed via integrations)
  geminiModel: 'gemini-2.0-flash-exp' | 'gemini-1.5-pro' | 'gemini-1.5-flash';
  
  // Local AI - endpoint config (keys managed via integrations or Supabase secrets)
  localEndpoint: string;
  localModel: string;
  
  // N8N Integration - webhook config (keys managed via integrations or Supabase secrets)
  n8nWebhookUrl: string;
  n8nWebhookType: 'chat' | 'generic';
  n8nTriggerMode: 'always' | 'keywords' | 'fallback';
  n8nTriggerKeywords: string[];
  
  // System prompt
  systemPrompt: string;
  
  // Tool Calling (Agentic AI)
  toolCallingEnabled: boolean;
  /** Optional allow-list of skill names (FlowPilot action skills). Empty = all external skills. */
  allowedSkillNames: string[];
  firecrawlSearchEnabled: boolean;
  humanHandoffEnabled: boolean;
  sentimentDetectionEnabled: boolean;
  sentimentThreshold: number; // 0-10, triggers handoff when exceeded
  localSupportsToolCalling: boolean; // Whether local AI supports OpenAI-compatible tool calling
  
  // General Knowledge
  allowGeneralKnowledge: boolean; // Allow AI to use its own knowledge beyond page content
  
  // Widget settings
  widgetEnabled: boolean;
  widgetPosition: 'bottom-right' | 'bottom-left';
  widgetButtonText: string;
  widgetStyle: ChatWidgetStyle;
  widgetSize: ChatWidgetSize;
  widgetMaxPrompts: number;
  widgetShowOnMobile: boolean;
  
  // Landing page
  landingPageEnabled: boolean;
  
  // Block
  blockEnabled: boolean;
  
  // Privacy & Compliance
  saveConversations: boolean;
  anonymizeData: boolean;
  auditLogging: boolean;
  dataRetentionDays: number;
  
  // Knowledge Base (CAG)
  includeContentAsContext: boolean;
  contentContextMaxTokens: number;
  includedPageSlugs: string[];
  
  // KB Articles in context
  includeKbArticles: boolean;
  
  // Context indicator
  showContextIndicator: boolean;
  
  // Feedback
  feedbackEnabled: boolean;
  
  // Suggested prompts (shown in empty state)
  suggestedPrompts: string[];
  
  // Live agent indicator settings
  showLiveAgentBanner: boolean;
  liveAgentIconStyle: 'avatar' | 'person' | 'headphones';
  
  // Chat appearance
  showChatIcons: boolean;
  
  // FlowPilot integration
  showEscalationsInCopilot: boolean;
  showPublicChatsInCopilot: boolean;
  
  // Speech — STT & TTS
  sttProvider: ChatSttProvider;
  sttLocalEndpoint: string;  // OpenAI-compatible Whisper endpoint
  sttLocalModel: string;
  ttsProvider: ChatTtsProvider;
  ttsLocalEndpoint: string;  // OpenAI-compatible TTS endpoint
  ttsLocalModel: string;
  ttsVoice: string;          // Voice ID (e.g. 'alloy', 'shimmer')
  ttsAutoPlay: boolean;      // Auto-play TTS in check-in mode
}

export const defaultChatSettings: ChatSettings = {
  enabled: false,
  title: 'AI Assistant',
  placeholder: 'Ask a question...',
  welcomeMessage: 'Hi! How can I help you today?',
  aiProvider: 'openai',
  openaiModel: 'gpt-4o-mini',
  openaiBaseUrl: 'https://api.openai.com/v1',
  geminiModel: 'gemini-2.0-flash-exp',
  localEndpoint: '',
  localModel: '',
  n8nWebhookUrl: '',
  n8nWebhookType: 'chat',
  n8nTriggerMode: 'always',
  n8nTriggerKeywords: [],
  systemPrompt: 'You are a helpful AI assistant. Always respond in the same language the user writes in.',
  toolCallingEnabled: false,
  allowedSkillNames: [],
  firecrawlSearchEnabled: false,
  humanHandoffEnabled: false,
  sentimentDetectionEnabled: true,
  sentimentThreshold: 7,
  localSupportsToolCalling: false,
  allowGeneralKnowledge: false,
  widgetEnabled: false,
  widgetPosition: 'bottom-right',
  widgetButtonText: 'Chat with us',
  widgetStyle: 'floating',
  widgetSize: 'md',
  widgetMaxPrompts: 3,
  widgetShowOnMobile: true,
  landingPageEnabled: false,
  blockEnabled: true,
  saveConversations: true,
  anonymizeData: false,
  auditLogging: true,
  dataRetentionDays: 90,
  includeContentAsContext: false,
  contentContextMaxTokens: 50000,
  includedPageSlugs: [],
  includeKbArticles: false,
  showContextIndicator: true,
  feedbackEnabled: true,
  suggestedPrompts: [
    'What can you help me with?',
    'Tell me about your services',
    'How do I book an appointment?',
  ],
  showLiveAgentBanner: true,
  liveAgentIconStyle: 'avatar',
  showChatIcons: true,
  showEscalationsInCopilot: false,
  showPublicChatsInCopilot: false,
  sttProvider: 'browser',
  sttLocalEndpoint: '',
  sttLocalModel: 'whisper-1',
  ttsProvider: 'none',
  ttsLocalEndpoint: '',
  ttsLocalModel: 'tts-1',
  ttsVoice: 'alloy',
  ttsAutoPlay: false,
};

// Generic hook for fetching settings
function useSiteSettings<T>(key: string, defaultValue: T) {
  return useQuery({
    queryKey: ['site-settings', key],
    queryFn: async () => {
      const { data, error } = await supabase
        .from('site_settings')
        .select('value')
        .eq('key', key)
        .maybeSingle();

      if (error) throw error;
      if (!data?.value) return defaultValue;
      // Always merge with defaults so new fields get their default values
      return { ...defaultValue, ...(data.value as unknown as T) };
    },
    staleTime: 1000 * 60 * 5,
  });
}

// Generic hook for updating settings
function useUpdateSiteSettings<T>(key: string, successMessage: string) {
  const queryClient = useQueryClient();
  const { toast } = useToast();

  return useMutation({
    mutationFn: async (settings: T) => {
      const { data: existing } = await supabase
        .from('site_settings')
        .select('id')
        .eq('key', key)
        .maybeSingle();

      const jsonValue = settings as unknown as Json;

      if (existing) {
        const { error } = await supabase
          .from('site_settings')
          .update({ 
            value: jsonValue,
            updated_at: new Date().toISOString()
          })
          .eq('key', key);

        if (error) throw error;
      } else {
        const { error } = await supabase
          .from('site_settings')
          .insert({ 
            key, 
            value: jsonValue
          });

        if (error) throw error;
      }

      return settings;
    },
    onSuccess: (settings) => {
      queryClient.setQueryData(['site-settings', key], settings);
      toast({
        title: 'Saved',
        description: successMessage,
      });
    },
    onError: (error) => {
      toast({
        title: 'Error',
        description: 'Could not save settings.',
        variant: 'destructive',
      });
      logger.error(`Failed to update ${key} settings:`, error);
    },
  });
}


// SEO hooks
export function useSeoSettings() {
  return useSiteSettings<SeoSettings>('seo', defaultSeoSettings);
}

export function useUpdateSeoSettings() {
  return useUpdateSiteSettings<SeoSettings>('seo', 'SEO settings have been updated.');
}

// Performance hooks
export function usePerformanceSettings() {
  return useSiteSettings<PerformanceSettings>('performance', defaultPerformanceSettings);
}

export function useUpdatePerformanceSettings() {
  return useUpdateSiteSettings<PerformanceSettings>('performance', 'Performance settings have been updated.');
}

// Branding hooks
export function useBrandingSettings() {
  return useSiteSettings<BrandingSettings>('branding', defaultBrandingSettings);
}

export function useUpdateBrandingSettings() {
  return useUpdateSiteSettings<BrandingSettings>('branding', 'Branding settings have been updated.');
}

// Custom Scripts hooks
export function useCustomScriptsSettings() {
  return useSiteSettings<CustomScriptsSettings>('custom_scripts', defaultCustomScriptsSettings);
}

export function useUpdateCustomScriptsSettings() {
  return useUpdateSiteSettings<CustomScriptsSettings>('custom_scripts', 'Script settings have been updated.');
}

// Cookie Banner hooks
export function useCookieBannerSettings() {
  return useSiteSettings<CookieBannerSettings>('cookie_banner', defaultCookieBannerSettings);
}

export function useUpdateCookieBannerSettings() {
  return useUpdateSiteSettings<CookieBannerSettings>('cookie_banner', 'Cookie settings have been updated.');
}

// Maintenance hooks
export function useMaintenanceSettings() {
  return useSiteSettings<MaintenanceSettings>('maintenance', defaultMaintenanceSettings);
}

export function useUpdateMaintenanceSettings() {
  return useUpdateSiteSettings<MaintenanceSettings>('maintenance', 'Maintenance settings have been updated.');
}

// Chat hooks
export function useChatSettings() {
  return useSiteSettings<ChatSettings>('chat', defaultChatSettings);
}

export function useUpdateChatSettings() {
  return useUpdateSiteSettings<ChatSettings>('chat', 'Chat settings have been updated.');
}

// General settings hooks
export function useGeneralSettings() {
  return useSiteSettings<GeneralSettings>('general', defaultGeneralSettings);
}

export function useUpdateGeneralSettings() {
  return useUpdateSiteSettings<GeneralSettings>('general', 'General settings have been updated.');
}

// Blog settings
export interface BlogSettings {
  enabled: boolean;
  postsPerPage: number;
  showAuthorBio: boolean;
  showReadingTime: boolean;
  showReviewer: boolean;
  archiveTitle: string;
  archiveSlug: string;
  rssEnabled: boolean;
  rssTitle: string;
  rssDescription: string;
}

const defaultBlogSettings: BlogSettings = {
  enabled: true,
  postsPerPage: 10,
  showAuthorBio: true,
  showReadingTime: true,
  showReviewer: false,
  archiveTitle: 'Blogg',
  archiveSlug: 'blog',
  rssEnabled: true,
  rssTitle: 'RSS Feed',
  rssDescription: 'Latest posts from our blog',
};

export function useBlogSettings() {
  return useSiteSettings<BlogSettings>('blog', defaultBlogSettings);
}

export function useUpdateBlogSettings() {
  return useUpdateSiteSettings<BlogSettings>('blog', 'Blog settings have been updated.');
}


// AEO (Answer Engine Optimization) settings
export type SchemaOrgType = 
  | 'Organization'
  | 'LocalBusiness'
  | 'ProfessionalService'
  | 'MedicalOrganization'
  | 'EducationalOrganization'
  | 'GovernmentOrganization'
  | 'Corporation';

export interface BusinessHours {
  dayOfWeek: string[];
  opens: string;
  closes: string;
}

export interface AeoSettings {
  // General
  enabled: boolean;
  organizationName: string;
  shortDescription: string;
  contactEmail: string;
  primaryLanguage: string;
  
  // llms.txt configuration
  llmsTxtEnabled: boolean;
  llmsTxtExcludedSlugs: string[];
  llmsFullTxtEnabled: boolean;
  maxWordsPerPage: number;
  
  // Schema.org / JSON-LD
  schemaOrgEnabled: boolean;
  schemaOrgType: SchemaOrgType;
  schemaOrgLogo: string;
  socialProfiles: string[];
  
  // LocalBusiness specific
  businessHours: BusinessHours[];
  priceRange: string;
  
  // FAQ Schema (auto-generated from accordion blocks)
  faqSchemaEnabled: boolean;
  
  // Article Schema for blog posts
  articleSchemaEnabled: boolean;
  
  // Sitemap
  sitemapEnabled: boolean;
  sitemapChangefreq: 'always' | 'hourly' | 'daily' | 'weekly' | 'monthly' | 'yearly' | 'never';
  sitemapPriority: number;
}

const defaultAeoSettings: AeoSettings = {
  enabled: false,
  organizationName: '',
  shortDescription: '',
  contactEmail: '',
  primaryLanguage: 'en',
  llmsTxtEnabled: true,
  llmsTxtExcludedSlugs: [],
  llmsFullTxtEnabled: true,
  maxWordsPerPage: 2000,
  schemaOrgEnabled: true,
  schemaOrgType: 'Organization',
  schemaOrgLogo: '',
  socialProfiles: [],
  businessHours: [],
  priceRange: '',
  faqSchemaEnabled: true,
  articleSchemaEnabled: true,
  sitemapEnabled: true,
  sitemapChangefreq: 'weekly',
  sitemapPriority: 0.5,
};

export function useAeoSettings() {
  return useSiteSettings<AeoSettings>('aeo', defaultAeoSettings);
}

export function useUpdateAeoSettings() {
  return useUpdateSiteSettings<AeoSettings>('aeo', 'AEO settings have been updated.');
}

// System AI settings (internal AI tools: text generation, company enrichment, lead qualification, etc.)
export type SystemAiProvider = 'openai' | 'gemini' | 'anthropic' | 'local';

export type OpenAiModel = 'gpt-4.1' | 'gpt-4.1-mini' | 'gpt-4.1-nano';
export type GeminiModel = 'gemini-2.5-pro' | 'gemini-2.5-flash' | 'gemini-2.0-flash-exp' | 'gemini-1.5-pro' | 'gemini-1.5-flash';
export type AnthropicModel = 'claude-sonnet-4-20250514' | 'claude-opus-4-20250514' | 'claude-3-5-haiku-20241022';

export interface SystemAiSettings {
  provider: SystemAiProvider;
  openaiModel: OpenAiModel;
  openaiReasoningModel: OpenAiModel;
  geminiModel: GeminiModel;
  geminiReasoningModel: GeminiModel;
  anthropicModel: AnthropicModel;
  anthropicReasoningModel: AnthropicModel;
  // Content generation preferences
  defaultTone: 'professional' | 'friendly' | 'formal';
  defaultLanguage: string;
}

const defaultSystemAiSettings: SystemAiSettings = {
  provider: 'openai',
  openaiModel: 'gpt-4.1-mini',
  openaiReasoningModel: 'gpt-4.1',
  geminiModel: 'gemini-2.5-flash',
  geminiReasoningModel: 'gemini-2.5-pro',
  anthropicModel: 'claude-sonnet-4-20250514',
  anthropicReasoningModel: 'claude-sonnet-4-20250514',
  defaultTone: 'professional',
  defaultLanguage: 'en',
};

export function useSystemAiSettings() {
  return useSiteSettings<SystemAiSettings>('system_ai', defaultSystemAiSettings);
}

export function useUpdateSystemAiSettings() {
  return useUpdateSiteSettings<SystemAiSettings>('system_ai', 'System AI settings have been updated.');
}

// Store / Commerce settings
export interface StoreSettings {
  currency: string;
  taxRate: number;
  taxDisplay: 'inclusive' | 'exclusive' | 'hidden';
  taxLabel: string;
  storeName: string;
}

export const defaultStoreSettings: StoreSettings = {
  currency: 'USD',
  taxRate: 0,
  taxDisplay: 'hidden',
  taxLabel: 'VAT',
  storeName: '',
};

export function useStoreSettings() {
  return useSiteSettings<StoreSettings>('store', defaultStoreSettings);
}

export function useUpdateStoreSettings() {
  return useUpdateSiteSettings<StoreSettings>('store', 'Store settings have been updated.');
}

// Autonomy Schedule settings
export interface AutonomyScheduleSettings {
  timezone: string;
  heartbeatEnabled: boolean;
  heartbeatHours: number[]; // e.g. [0, 12] = twice daily
  briefingEnabled: boolean;
  briefingHour: number; // local hour, e.g. 8
  learnEnabled: boolean;
  learnHour: number; // local hour, e.g. 3
}

export const defaultAutonomyScheduleSettings: AutonomyScheduleSettings = {
  timezone: 'Europe/Stockholm',
  heartbeatEnabled: true,
  heartbeatHours: [0, 12],
  briefingEnabled: true,
  briefingHour: 8,
  learnEnabled: true,
  learnHour: 3,
};

export function useAutonomyScheduleSettings() {
  return useSiteSettings<AutonomyScheduleSettings>('autonomy_schedule', defaultAutonomyScheduleSettings);
}

export function useUpdateAutonomyScheduleSettings() {
  return useUpdateSiteSettings<AutonomyScheduleSettings>('autonomy_schedule', 'Autonomy schedule has been updated.');
}

// Re-export modules hooks for convenience
export { useModules, useUpdateModules, useIsModuleEnabled, useEnabledModules } from './useModules';
export type { ModulesSettings, ModuleConfig } from './useModules';
