import apiClient from '../../../shared/services/apiClient';

export type PageType = 'home' | 'contact' | 'about' | 'footer' | 'seo' | 'privacy' | 'terms' | 'refunds' | 'cancellation' | 'accessibility' | 'faq';

export interface PageContent {
  id?: number;
  page_type: PageType;
  title?: string;
  subtitle?: string;
  description?: string;
  content?: string;
  meta_title?: string;
  meta_description?: string;
  meta_keywords?: string;
  og_title?: string;
  og_description?: string;
  og_image?: string;
  canonical_url?: string;
  contact_info?: {
    phone?: string;
    email?: string;
    address?: string;
  };
  contact_icons?: {
    hero?: string;
    email?: string;
    phone?: string;
    location?: string;
    name_field?: string;
    email_field?: string;
    phone_field?: string;
    subject_field?: string;
    message_field?: string;
    submit_button?: string;
  };
  map_url?: string;
  social_links?: {
    facebook?: string;
    twitter?: string;
    instagram?: string;
    linkedin?: string;
    youtube?: string;
  };
  footer_links?: {
    quick_links?: Array<{ label: string; url: string }>;
    support_links?: Array<{ label: string; url: string }>;
  };
  badges?: Array<{ text: string; icon: string }>;
  copyright_text?: string;
  hero_title?: string;
  hero_subtitle?: string;
  hero_image?: string;
  hero_video_url?: string;
  hero_video_poster?: string;
  story_content?: string;
  values?: Array<{ icon?: string; title: string; description: string }>;
  features?: Array<{ icon?: string; title: string; description: string; image?: string }>;
  features_section_title?: string;
  features_section_subtitle?: string;
  about_hero_image?: string;
  about_hero_icon?: string;
  about_contact_icons?: {
    location?: string;
    phone?: string;
    email?: string;
  };
  about_learn_more_icon?: string;
  mission?: string;
  vision?: string;
  team?: Array<{ name: string; role: string; image?: string; bio?: string; social_links?: { linkedin?: string; twitter?: string; email?: string } }>;
  timeline?: Array<{ year: string; title: string; description: string; image?: string }>;
  achievements?: Array<{ icon?: string; title: string; description: string; year?: string; image?: string }>;
  
  luxury_section_title?: string;
  luxury_section_subtitle?: string;
  luxury_section_image?: string;
  luxury_features?: Array<{ icon?: string; title: string; description: string }>;
  luxury_gallery_section_title?: string;
  luxury_gallery_section_subtitle?: string;
  luxury_gallery?: Array<string>;
  luxury_testimonials_section_title?: string;
  luxury_testimonials_section_subtitle?: string;
  luxury_testimonials?: Array<{ name: string; title?: string; quote: string; image?: string }>;
  amenities_section_title?: string;
  amenities_section_subtitle?: string;
  amenities?: Array<{ icon?: string; title: string; description: string; image?: string }>;
  testimonials_section_title?: string;
  testimonials_section_subtitle?: string;
  testimonials?: Array<{ name: string; role: string; image?: string; rating: number; comment: string }>;
  gallery_section_title?: string;
  gallery_section_subtitle?: string;
  gallery_images?: Array<string>;
  about_preview_title?: string;
  about_preview_subtitle?: string;
  about_preview_content?: string;
  about_preview_image?: string;
  stats?: Array<{ number: string; label: string; icon?: string }>;
  stats_section_title?: string;
  stats_section_subtitle?: string;
  rooms_section_title?: string;
  rooms_section_subtitle?: string;
  rooms_section_button_text?: string;
  rooms_section_button_link?: string;
  rooms_section_enabled?: boolean;
  luxury_services_section_title?: string;
  luxury_services_section_subtitle?: string;
  services_section_button_text?: string;
  services_section_button_link?: string;
  services_section_limit?: number;
  services_fallback_icon?: string;
  luxury_services?: Array<{ 
    icon?: string; 
    title: string; 
    description: string; 
    image?: string;
    slug?: string;
    category?: string;
    content?: string;
    sections?: Array<{
      type: 'hero' | 'text' | 'image' | 'gallery' | 'quote' | 'features' | 'cta' | 'video';
      title?: string;
      content?: string;
      image?: string;
      images?: string[];
      quote?: string;
      author?: string;
      features?: Array<{ title: string; description: string; icon?: string }>;
      cta_text?: string;
      cta_link?: string;
      video_url?: string;
      alignment?: 'left' | 'center' | 'right';
      is_visible?: boolean;
    }>;
    meta_title?: string;
    meta_description?: string;
    meta_keywords?: string;
  }>;
  luxury_experiences_section_title?: string;
  luxury_experiences_section_subtitle?: string;
  luxury_experiences?: Array<{ icon?: string; title: string; description: string; image?: string }>;
  awards_section_title?: string;
  awards_section_subtitle?: string;
  awards?: Array<{ icon?: string; title: string; description: string; image?: string; year?: string }>;
  cta_title?: string;
  cta_subtitle?: string;
  cta_button_text?: string;
  cta_button_link?: string;
  cta_image?: string;
  partners_section_title?: string;
  partners_section_subtitle?: string;
  partners?: Array<{ name: string; logo: string; link?: string }>;
  sections_enabled?: {
    features?: boolean;
    luxury?: boolean;
    gallery?: boolean;
    testimonials?: boolean;
    stats?: boolean;
    amenities?: boolean;
    about_preview?: boolean;
    services?: boolean;
    experiences?: boolean;
    awards?: boolean;
    cta?: boolean;
    partners?: boolean;
    rooms?: boolean;
    newsletter?: boolean;
    trust_badges?: boolean;
    promotions?: boolean;
    blog?: boolean;
  };
  newsletter_section_title?: string;
  newsletter_section_subtitle?: string;
  newsletter_placeholder?: string;
  newsletter_button_text?: string;
  newsletter_enabled?: boolean;
  trust_badges_section_title?: string;
  trust_badges_section_subtitle?: string;
  trust_badges?: Array<{ name: string; logo: string; description?: string; link?: string }>;
  trust_badges_enabled?: boolean;
  promotions_section_title?: string;
  promotions_section_subtitle?: string;
  promotions?: Array<{ 
    title: string; 
    description: string; 
    image?: string; 
    discount?: string; 
    valid_until?: string; 
    link?: string;
    button_text?: string;
  }>;
  promotions_enabled?: boolean;
  blog_section_title?: string;
  blog_section_subtitle?: string;
  blog_posts_limit?: number;
  blog_enabled?: boolean;
  is_active?: boolean;
  created_at?: string;
  updated_at?: string;
}

export interface PageContentResponse {
  status: string;
  data: {
    page_content?: PageContent | null;
    page_contents?: PageContent[];
  };
  message?: string;
}

export interface UpdatePageContentData {
  title?: string;
  subtitle?: string;
  description?: string;
  content?: string;
  meta_title?: string;
  meta_description?: string;
  meta_keywords?: string;
  og_title?: string;
  og_description?: string;
  og_image?: string;
  canonical_url?: string;
  contact_info?: {
    phone?: string;
    email?: string;
    address?: string;
  };
  contact_icons?: {
    hero?: string;
    email?: string;
    phone?: string;
    location?: string;
    name_field?: string;
    email_field?: string;
    phone_field?: string;
    subject_field?: string;
    message_field?: string;
    submit_button?: string;
  };
  map_url?: string;
  social_links?: {
    facebook?: string;
    twitter?: string;
    instagram?: string;
    linkedin?: string;
    youtube?: string;
  };
  footer_links?: {
    quick_links?: Array<{ label: string; url: string }>;
    support_links?: Array<{ label: string; url: string }>;
  };
  badges?: Array<{ text: string; icon: string }>;
  copyright_text?: string;
  hero_title?: string;
  hero_subtitle?: string;
  hero_image?: string;
  hero_video_url?: string;
  hero_video_poster?: string;
  story_content?: string;
  values?: Array<{ icon?: string; title: string; description: string }>;
  features?: Array<{ icon?: string; title: string; description: string; image?: string }>;
  features_section_title?: string;
  features_section_subtitle?: string;
  about_hero_image?: string;
  about_hero_icon?: string;
  about_contact_icons?: {
    location?: string;
    phone?: string;
    email?: string;
  };
  about_learn_more_icon?: string;
  mission?: string;
  vision?: string;
  team?: Array<{ name: string; role: string; image?: string; bio?: string; social_links?: { linkedin?: string; twitter?: string; email?: string } }>;
  timeline?: Array<{ year: string; title: string; description: string; image?: string }>;
  achievements?: Array<{ icon?: string; title: string; description: string; year?: string; image?: string }>;
  
  luxury_section_title?: string;
  luxury_section_subtitle?: string;
  luxury_section_image?: string;
  luxury_features?: Array<{ icon?: string; title: string; description: string }>;
  luxury_gallery_section_title?: string;
  luxury_gallery_section_subtitle?: string;
  luxury_gallery?: Array<string>;
  luxury_testimonials_section_title?: string;
  luxury_testimonials_section_subtitle?: string;
  luxury_testimonials?: Array<{ name: string; title?: string; quote: string; image?: string }>;
  amenities_section_title?: string;
  amenities_section_subtitle?: string;
  amenities?: Array<{ icon?: string; title: string; description: string; image?: string }>;
  testimonials_section_title?: string;
  testimonials_section_subtitle?: string;
  testimonials?: Array<{ name: string; role: string; image?: string; rating: number; comment: string }>;
  gallery_section_title?: string;
  gallery_section_subtitle?: string;
  gallery_images?: Array<string>;
  about_preview_title?: string;
  about_preview_subtitle?: string;
  about_preview_content?: string;
  about_preview_image?: string;
  stats?: Array<{ number: string; label: string; icon?: string }>;
  stats_section_title?: string;
  stats_section_subtitle?: string;
  rooms_section_title?: string;
  rooms_section_subtitle?: string;
  rooms_section_button_text?: string;
  rooms_section_button_link?: string;
  rooms_section_enabled?: boolean;
  luxury_services_section_title?: string;
  luxury_services_section_subtitle?: string;
  services_section_button_text?: string;
  services_section_button_link?: string;
  services_section_limit?: number;
  services_fallback_icon?: string;
  luxury_services?: Array<{ 
    icon?: string; 
    title: string; 
    description: string; 
    image?: string;
    slug?: string;
    category?: string;
    content?: string;
    sections?: Array<{
      type: 'hero' | 'text' | 'image' | 'gallery' | 'quote' | 'features' | 'cta' | 'video';
      title?: string;
      content?: string;
      image?: string;
      images?: string[];
      quote?: string;
      author?: string;
      features?: Array<{ title: string; description: string; icon?: string }>;
      cta_text?: string;
      cta_link?: string;
      video_url?: string;
      alignment?: 'left' | 'center' | 'right';
      is_visible?: boolean;
    }>;
    meta_title?: string;
    meta_description?: string;
    meta_keywords?: string;
  }>;
  luxury_experiences_section_title?: string;
  luxury_experiences_section_subtitle?: string;
  luxury_experiences?: Array<{ icon?: string; title: string; description: string; image?: string }>;
  awards_section_title?: string;
  awards_section_subtitle?: string;
  awards?: Array<{ icon?: string; title: string; description: string; image?: string; year?: string }>;
  cta_title?: string;
  cta_subtitle?: string;
  cta_button_text?: string;
  cta_button_link?: string;
  cta_image?: string;
  partners_section_title?: string;
  partners_section_subtitle?: string;
  partners?: Array<{ name: string; logo: string; link?: string }>;
  sections_enabled?: {
    features?: boolean;
    luxury?: boolean;
    gallery?: boolean;
    testimonials?: boolean;
    stats?: boolean;
    amenities?: boolean;
    about_preview?: boolean;
    services?: boolean;
    experiences?: boolean;
    awards?: boolean;
    cta?: boolean;
    partners?: boolean;
    rooms?: boolean;
    newsletter?: boolean;
    trust_badges?: boolean;
    promotions?: boolean;
    blog?: boolean;
  };
  newsletter_section_title?: string;
  newsletter_section_subtitle?: string;
  newsletter_placeholder?: string;
  newsletter_button_text?: string;
  newsletter_enabled?: boolean;
  trust_badges_section_title?: string;
  trust_badges_section_subtitle?: string;
  trust_badges?: Array<{ name: string; logo: string; description?: string; link?: string }>;
  trust_badges_enabled?: boolean;
  promotions_section_title?: string;
  promotions_section_subtitle?: string;
  promotions?: Array<{ 
    title: string; 
    description: string; 
    image?: string; 
    discount?: string; 
    valid_until?: string; 
    link?: string;
    button_text?: string;
  }>;
  promotions_enabled?: boolean;
  blog_section_title?: string;
  blog_section_subtitle?: string;
  blog_posts_limit?: number;
  blog_enabled?: boolean;
  is_active?: boolean;
}

const pageContentService = {
  
  getAllPageContents: async (): Promise<PageContentResponse> => {
    const response = await apiClient.get<PageContentResponse>('/page-content');
    return response.data;
  },

  
  getPageContent: async (pageType: PageType): Promise<PageContentResponse> => {
    const response = await apiClient.get<PageContentResponse>(`/page-content/${pageType}`);
    return response.data;
  },

  
  getHomeContent: async (): Promise<PageContentResponse> => {
    const response = await apiClient.get<PageContentResponse>('/home');
    return response.data;
  },

  
  getAboutContent: async (): Promise<PageContentResponse> => {
    const response = await apiClient.get<PageContentResponse>('/about');
    return response.data;
  },

  
  getContactContent: async (): Promise<PageContentResponse> => {
    const response = await apiClient.get<PageContentResponse>('/contact-content');
    return response.data;
  },

  
  getFooterContent: async (): Promise<PageContentResponse> => {
    const response = await apiClient.get<PageContentResponse>('/footer');
    return response.data;
  },

  getPrivacyContent: async (): Promise<PageContentResponse> => {
    const response = await apiClient.get<PageContentResponse>('/privacy');
    return response.data;
  },

  getTermsContent: async (): Promise<PageContentResponse> => {
    const response = await apiClient.get<PageContentResponse>('/terms');
    return response.data;
  },

  getRefundsContent: async (): Promise<PageContentResponse> => {
    const response = await apiClient.get<PageContentResponse>('/refunds');
    return response.data;
  },

  getCancellationContent: async (): Promise<PageContentResponse> => {
    const response = await apiClient.get<PageContentResponse>('/cancellation');
    return response.data;
  },

  getAccessibilityContent: async (): Promise<PageContentResponse> => {
    const response = await apiClient.get<PageContentResponse>('/accessibility');
    return response.data;
  },

  getFAQContent: async (): Promise<PageContentResponse> => {
    const response = await apiClient.get<PageContentResponse>('/faq');
    return response.data;
  },

  
  updatePageContent: async (
    pageType: PageType,
    data: UpdatePageContentData
  ): Promise<PageContentResponse> => {
    
    const updateData: Record<string, unknown> = { ...data };
    
    
    if (data.contact_info) {
      const contactInfo = {
        phone: data.contact_info.phone || '',
        email: data.contact_info.email || '',
        address: data.contact_info.address || '',
      };
      updateData.contact_info = contactInfo; 
    }
    
    
    if (data.social_links) {
      const socialLinks = {
        facebook: data.social_links.facebook || '',
        twitter: data.social_links.twitter || '',
        instagram: data.social_links.instagram || '',
        linkedin: data.social_links.linkedin || '',
        youtube: data.social_links.youtube || '',
      };
      updateData.social_links = socialLinks; 
    }
    
    
    if (data.footer_links) {
      updateData.footer_links = {
        quick_links: data.footer_links.quick_links || [],
        support_links: data.footer_links.support_links || [],
      };
    }
    
    
    if (data.badges) {
      updateData.badges = data.badges; 
    }
    
    
    if (data.values) {
      updateData.values = data.values; 
    }
    if (data.features) {
      updateData.features = data.features; 
    }
    
    
    if (data.luxury_features) {
      updateData.luxury_features = data.luxury_features; 
    }
    if (data.luxury_gallery) {
      updateData.luxury_gallery = data.luxury_gallery; 
    }
    if (data.luxury_testimonials) {
      updateData.luxury_testimonials = data.luxury_testimonials; 
    }
    if (data.amenities) {
      updateData.amenities = data.amenities; 
    }
    if (data.testimonials) {
      updateData.testimonials = data.testimonials; 
    }
    if (data.gallery_images) {
      updateData.gallery_images = data.gallery_images; 
    }
    if (data.stats) {
      updateData.stats = data.stats; 
    }
    if (data.luxury_services) {
      updateData.luxury_services = data.luxury_services; 
    }
    if (data.luxury_experiences) {
      updateData.luxury_experiences = data.luxury_experiences; 
    }
    if (data.awards) {
      updateData.awards = data.awards; 
    }
    if (data.partners) {
      updateData.partners = data.partners; 
    }
    if (data.team) {
      updateData.team = data.team; 
    }
    if (data.timeline) {
      updateData.timeline = data.timeline; 
    }
    if (data.achievements) {
      updateData.achievements = data.achievements; 
    }
    if (data.trust_badges) {
      updateData.trust_badges = data.trust_badges; 
    }
    if (data.promotions) {
      updateData.promotions = data.promotions; 
    }
    if (data.sections_enabled) {
      updateData.sections_enabled = data.sections_enabled; 
    }

    const response = await apiClient.put<PageContentResponse>(
      `/page-content/${pageType}`,
      updateData
    );
    return response.data;
  },

  
  uploadImage: async (
    file: File
  ): Promise<{ success: boolean; data: { image_url: string; full_url: string }; message: string }> => {
    const formData = new FormData();
    formData.append('image', file);
    
    
    const response = await apiClient.post('/page-content/upload', formData);
    return response.data;
  },
};

export default pageContentService;

