import api, { ApiResponse } from './api';

// TypeScript interfaces for each section's content shape
export interface HeroContent {
  headline: string;
  description: string;
  ctaButtonText: string;
  helperText: string;
}

export interface FeatureItem {
  iconPath: string;
  title: string;
  description: string;
}

export interface FeaturedAgentItem {
  name: string;
  role: string;
  rating: string;
  location: string;
  experience: string;
  imageUrl: string;
}

export interface FeaturesContent {
  title: string;
  subtitle: string;
  features: FeatureItem[];
  featuredAgents?: FeaturedAgentItem[];
}

export interface ProfessionalItem {
  name: string;
  subtitle: string;
  location: string;
  experience: string;
  expertise: string[];
  imageUrl: string;
}

export interface TopProfessionalsContent {
  title: string;
  ctaText: string;
  ctaButtonText: string;
  agents: ProfessionalItem[];
  lenders: ProfessionalItem[];
}

export interface TestimonialItem {
  rating: number;
  title: string;
  content: string;
  author: string;
  role: string;
}

export interface StatItem {
  iconPath: string;
  boldText: string;
  normalText: string;
}

export interface TestimonialsContent {
  title: string;
  subtitle: string;
  ratingInfo: string;
  stats: StatItem[];
  testimonials: TestimonialItem[];
}

export interface AboutHeroContent {
  badge: string;
  headline: string;
  description: string;
  ctaButtonText: string;
  bannerImageUrl: string;
  bannerOverlayText: string;
}

export interface AboutStatItem {
  value: string;
  label: string;
}

export interface AboutStatsContent {
  stats: AboutStatItem[];
}

export interface AboutFeatureItem {
  iconPath: string;
  title: string;
  description: string;
}

export interface AboutFeaturesContent {
  badge: string;
  features: AboutFeatureItem[];
}

export interface AboutTeamMember {
  name: string;
  role: string;
  imageUrl: string;
}

export interface AboutTeamContent {
  title: string;
  subtitle: string;
  members: AboutTeamMember[];
}

export interface AboutCtaContent {
  title: string;
  description: string;
  buttonText: string;
}

export interface FaqCategoryItem {
  id: string;
  label: string;
}

export interface FaqItem {
  id: number;
  icon: string;
  question: string;
  answer: string;
  category: string;
}

export interface FaqContent {
  pageTitle: string;
  categories: FaqCategoryItem[];
  faqs: FaqItem[];
  supportTitle: string;
  supportDescription: string;
}

export interface CmsContentRecord {
  id: string;
  pageSlug: string;
  sectionKey: string;
  content: HeroContent | FeaturesContent | TopProfessionalsContent | TestimonialsContent;
  isPublished: boolean;
  createdAt: string;
  updatedAt: string;
}

class CmsService {
  private basePath = '/cms';

  async getByPage(pageSlug: string): Promise<CmsContentRecord[]> {
    const response = await api.get<ApiResponse<CmsContentRecord[]>>(`${this.basePath}/page/${pageSlug}`);
    return response.data.data;
  }

  async upsert(data: { pageSlug: string; sectionKey: string; content: unknown; isPublished?: boolean }): Promise<CmsContentRecord> {
    const response = await api.put<ApiResponse<CmsContentRecord>>(`${this.basePath}/upsert`, data);
    return response.data.data;
  }
}

export const cmsService = new CmsService();
