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

export interface ServiceSection {
  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;
}

export interface Service {
  id: number;
  name: string;
  description?: string;
  price: number;
  unit?: string;
  category?: string;
  slug?: string;
  image?: string;
  content?: string;
  sections?: ServiceSection[] | string;  // Can be array or JSON string
  meta_title?: string;
  meta_description?: string;
  meta_keywords?: string;
  status: 'active' | 'inactive';
  is_active?: boolean;
  created_at?: string;
  updated_at?: string;
}

export interface ServiceListResponse {
  success: boolean;
  status?: string;
  data: {
    services: Service[];
    pagination?: {
      total: number;
      page: number;
      limit: number;
      totalPages: number;
    };
  };
  message?: string;
}

export interface CreateServiceData {
  name: string;
  description?: string;
  price: number;
  unit?: string;
  category?: string;
  slug?: string;
  image?: string;
  content?: string;
  sections?: ServiceSection[] | string;
  meta_title?: string;
  meta_description?: string;
  meta_keywords?: string;
  status?: 'active' | 'inactive';
}

export interface UpdateServiceData {
  name?: string;
  description?: string;
  price?: number;
  unit?: string;
  category?: string;
  slug?: string;
  image?: string;
  content?: string;
  sections?: ServiceSection[] | string;
  meta_title?: string;
  meta_description?: string;
  meta_keywords?: string;
  status?: 'active' | 'inactive';
}

export interface ServiceSearchParams {
  status?: string;
  search?: string;
  page?: number;
  limit?: number;
}

export const getServices = async (
  params: ServiceSearchParams = {}
): Promise<ServiceListResponse> => {
  const response = await apiClient.get('/services', { params });
  const data = response.data;
  // Handle both 'status: success' and 'success: true' formats
  return {
    success: data.status === 'success' || data.success === true,
    status: data.status,
    data: data.data || { services: [] },
    message: data.message,
  };
};

export const getServiceById = async (
  id: number
): Promise<{ success: boolean; data: { service: Service } }> => {
  const response = await apiClient.get(`/services/${id}`);
  const data = response.data;
  // Handle both 'status: success' and 'success: true' formats
  return {
    success: data.status === 'success' || data.success === true,
    data: data.data || {},
  };
};

export const getServiceBySlug = async (
  slug: string
): Promise<{ success: boolean; data: { service: Service } }> => {
  const response = await apiClient.get(`/services/slug/${slug}`);
  const data = response.data;
  // Handle both 'status: success' and 'success: true' formats
  return {
    success: data.status === 'success' || data.success === true,
    data: data.data || {},
  };
};

export const createService = async (
  data: CreateServiceData
): Promise<{ success: boolean; data: { service: Service }; message: string }> => {
  const response = await apiClient.post('/services', data);
  const responseData = response.data;
  // Handle both 'status: success' and 'success: true' formats
  return {
    success: responseData.status === 'success' || responseData.success === true,
    data: responseData.data || {},
    message: responseData.message || '',
  };
};

export const updateService = async (
  id: number,
  data: UpdateServiceData
): Promise<{ success: boolean; data: { service: Service }; message: string }> => {
  const response = await apiClient.put(`/services/${id}`, data);
  const responseData = response.data;
  // Handle both 'status: success' and 'success: true' formats
  return {
    success: responseData.status === 'success' || responseData.success === true,
    data: responseData.data || {},
    message: responseData.message || '',
  };
};

export const deleteService = async (
  id: number
): Promise<{ success: boolean; message: string }> => {
  const response = await apiClient.delete(`/services/${id}`);
  const data = response.data;
  // Handle both 'status: success' and 'success: true' formats
  return {
    success: data.status === 'success' || data.success === true,
    message: data.message || '',
  };
};

export const useService = async (data: {
  booking_id: number;
  service_id: number;
  quantity: number;
}): Promise<{ success: boolean; message: string }> => {
  const response = await apiClient.post('/services/use', data);
  const responseData = response.data;
  // Handle both 'status: success' and 'success: true' formats
  return {
    success: responseData.status === 'success' || responseData.success === true,
    message: responseData.message || '',
  };
};

export default {
  getServices,
  getServiceById,
  getServiceBySlug,
  createService,
  updateService,
  deleteService,
  useService,
};
