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

export interface BlogSection {
  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';
  background_color?: string;
  text_color?: string;
  is_visible?: boolean;
}

export interface BlogPost {
  id: number;
  title: string;
  slug: string;
  excerpt?: string;
  content?: string;
  featured_image?: string;
  author_id: number;
  author_name?: string;
  published_at?: string;
  is_published?: boolean;
  views_count: number;
  tags?: string[];
  meta_title?: string;
  meta_description?: string;
  meta_keywords?: string;
  sections?: BlogSection[];
  created_at: string;
  updated_at: string;
}

export interface BlogPostCreate {
  title: string;
  slug?: string;
  excerpt?: string;
  content: string;
  featured_image?: string;
  tags?: string[];
  meta_title?: string;
  meta_description?: string;
  meta_keywords?: string;
  is_published?: boolean;
  published_at?: string;
  sections?: BlogSection[];
}

export interface BlogPostUpdate {
  title?: string;
  slug?: string;
  excerpt?: string;
  content?: string;
  featured_image?: string;
  tags?: string[];
  meta_title?: string;
  meta_description?: string;
  meta_keywords?: string;
  is_published?: boolean;
  published_at?: string;
  sections?: BlogSection[];
}

export interface BlogListResponse {
  posts: BlogPost[];
  pagination: {
    page: number;
    limit: number;
    total: number;
    pages: number;
  };
  all_tags?: string[];
}

export interface BlogPostResponse {
  post: BlogPost;
}

class BlogService {
  // Public endpoints
  async getBlogPosts(params?: {
    page?: number;
    limit?: number;
    search?: string;
    tag?: string;
    published_only?: boolean;
  }): Promise<{ status: string; data: BlogListResponse }> {
    const response = await apiClient.get('/blog/', { params });
    return response.data;
  }

  async getBlogPostBySlug(slug: string): Promise<{ status: string; data: BlogPostResponse }> {
    const response = await apiClient.get(`/blog/${slug}`);
    return response.data;
  }

  // Admin endpoints
  async getAllBlogPosts(params?: {
    page?: number;
    limit?: number;
    search?: string;
    published?: boolean;
  }): Promise<{ status: string; data: BlogListResponse }> {
    const response = await apiClient.get('/blog/admin/posts', { params });
    return response.data;
  }

  async getBlogPostById(id: number): Promise<{ status: string; data: BlogPostResponse }> {
    const response = await apiClient.get(`/blog/admin/posts/${id}`);
    return response.data;
  }

  async createBlogPost(data: BlogPostCreate): Promise<{ status: string; data: BlogPostResponse; message?: string }> {
    const response = await apiClient.post('/blog/admin/posts', data);
    return response.data;
  }

  async updateBlogPost(id: number, data: BlogPostUpdate): Promise<{ status: string; data: BlogPostResponse; message?: string }> {
    const response = await apiClient.put(`/blog/admin/posts/${id}`, data);
    return response.data;
  }

  async deleteBlogPost(id: number): Promise<{ status: string; message?: string }> {
    const response = await apiClient.delete(`/blog/admin/posts/${id}`);
    return response.data;
  }

  async uploadBlogImage(file: File): Promise<{ status: string; data: { image_url: string; full_url: string }; message?: string }> {
    const formData = new FormData();
    formData.append('image', file);
    const response = await apiClient.post('/blog/admin/upload-image', formData, {
      headers: {
        'Content-Type': 'multipart/form-data',
      },
    });
    return response.data;
  }

  // Tag management endpoints
  async getAllTags(): Promise<{ status: string; data: { tags: Array<{ name: string; usage_count: number }>; total: number } }> {
    const response = await apiClient.get('/blog/admin/tags');
    return response.data;
  }

  async renameTag(oldTag: string, newTag: string): Promise<{ status: string; data: { old_tag: string; new_tag: string; updated_posts: number }; message?: string }> {
    const response = await apiClient.put('/blog/admin/tags/rename', null, {
      params: { old_tag: oldTag, new_tag: newTag },
    });
    return response.data;
  }

  async deleteTag(tag: string): Promise<{ status: string; data: { deleted_tag: string; updated_posts: number }; message?: string }> {
    const response = await apiClient.delete('/blog/admin/tags', {
      params: { tag },
    });
    return response.data;
  }
}

export const blogService = new BlogService();
export default blogService;

