/**
 * WooNooW Window API
 * 
 * Exposes React, hooks, components, and utilities to addon developers
 * via window.WooNooW object
 */

import React from 'react';
import ReactDOM from 'react-dom/client';
import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { toast } from 'sonner';

// UI Components
import { Button } from '@/components/ui/button';
import { Input } from '@/components/ui/input';
import { Label } from '@/components/ui/label';
import { Textarea } from '@/components/ui/textarea';
import { Switch } from '@/components/ui/switch';
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
import { Checkbox } from '@/components/ui/checkbox';
import { Badge } from '@/components/ui/badge';
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card';

// Settings Components
import { SettingsLayout } from '@/routes/Settings/components/SettingsLayout';
import { SettingsCard } from '@/routes/Settings/components/SettingsCard';
import { SettingsSection } from '@/routes/Settings/components/SettingsSection';

// Form Components
import { SchemaForm } from '@/components/forms/SchemaForm';
import { SchemaField } from '@/components/forms/SchemaField';

// Hooks
import { useModules } from '@/hooks/useModules';
import { useModuleSettings } from '@/hooks/useModuleSettings';

// Utils
import { api } from '@/lib/api';
import { __ } from '@/lib/i18n';

// Icons (commonly used)
import { 
  Settings, 
  Save, 
  Trash2, 
  Edit, 
  Plus, 
  X, 
  Check, 
  AlertCircle,
  Info,
  Loader2,
  ChevronDown,
  ChevronUp,
  ChevronLeft,
  ChevronRight,
} from 'lucide-react';

/**
 * WooNooW Window API Interface
 */
export interface WooNooWAPI {
  React: typeof React;
  ReactDOM: typeof ReactDOM;
  
  hooks: {
    useQuery: typeof useQuery;
    useMutation: typeof useMutation;
    useQueryClient: typeof useQueryClient;
    useModules: typeof useModules;
    useModuleSettings: typeof useModuleSettings;
  };
  
  components: {
    // Basic UI
    Button: typeof Button;
    Input: typeof Input;
    Label: typeof Label;
    Textarea: typeof Textarea;
    Switch: typeof Switch;
    Select: typeof Select;
    SelectContent: typeof SelectContent;
    SelectItem: typeof SelectItem;
    SelectTrigger: typeof SelectTrigger;
    SelectValue: typeof SelectValue;
    Checkbox: typeof Checkbox;
    Badge: typeof Badge;
    Card: typeof Card;
    CardContent: typeof CardContent;
    CardDescription: typeof CardDescription;
    CardFooter: typeof CardFooter;
    CardHeader: typeof CardHeader;
    CardTitle: typeof CardTitle;
    
    // Settings Components
    SettingsLayout: typeof SettingsLayout;
    SettingsCard: typeof SettingsCard;
    SettingsSection: typeof SettingsSection;
    
    // Form Components
    SchemaForm: typeof SchemaForm;
    SchemaField: typeof SchemaField;
  };
  
  icons: {
    Settings: typeof Settings;
    Save: typeof Save;
    Trash2: typeof Trash2;
    Edit: typeof Edit;
    Plus: typeof Plus;
    X: typeof X;
    Check: typeof Check;
    AlertCircle: typeof AlertCircle;
    Info: typeof Info;
    Loader2: typeof Loader2;
    ChevronDown: typeof ChevronDown;
    ChevronUp: typeof ChevronUp;
    ChevronLeft: typeof ChevronLeft;
    ChevronRight: typeof ChevronRight;
  };
  
  utils: {
    api: typeof api;
    toast: typeof toast;
    __: typeof __;
  };
}

/**
 * Initialize Window API
 * Exposes WooNooW API to window object for addon developers
 */
export function initializeWindowAPI() {
  const windowAPI: WooNooWAPI = {
    React,
    ReactDOM,
    
    hooks: {
      useQuery,
      useMutation,
      useQueryClient,
      useModules,
      useModuleSettings,
    },
    
    components: {
      Button,
      Input,
      Label,
      Textarea,
      Switch,
      Select,
      SelectContent,
      SelectItem,
      SelectTrigger,
      SelectValue,
      Checkbox,
      Badge,
      Card,
      CardContent,
      CardDescription,
      CardFooter,
      CardHeader,
      CardTitle,
      SettingsLayout,
      SettingsCard,
      SettingsSection,
      SchemaForm,
      SchemaField,
    },
    
    icons: {
      Settings,
      Save,
      Trash2,
      Edit,
      Plus,
      X,
      Check,
      AlertCircle,
      Info,
      Loader2,
      ChevronDown,
      ChevronUp,
      ChevronLeft,
      ChevronRight,
    },
    
    utils: {
      api,
      toast,
      __,
    },
  };
  
  // Expose to window
  (window as any).WooNooW = windowAPI;
  
  if ((window as any).WNW_API?.isDev) {
    console.log('✅ WooNooW API initialized for addon developers');
  }
}
