import apiClient from './client';

// Types
export interface Client {
  id: string;
  tenant_id: string;
  name: string;
  slug: string;
  type: 'company' | 'individual';
  industry: string | null;
  website: string | null;
  logo_url: string | null;
  contact_name: string | null;
  contact_email: string | null;
  contact_phone: string | null;
  address: string | null;
  notes: string | null;
  is_active: boolean;
  metadata: Record<string, any> | null;
  created_at: string;
  updated_at: string;
}

export interface Brand {
  id: string;
  tenant_id: string;
  client_id: string;
  client?: Client;
  name: string;
  slug: string;
  description: string | null;
  logo_url: string | null;
  primary_color: string | null;
  secondary_color: string | null;
  brand_voice: string | null;
  target_audience: string | null;
  keywords: string[] | null;
  guidelines_url: string | null;
  is_active: boolean;
  metadata: Record<string, any> | null;
  created_at: string;
  updated_at: string;
}

export interface Product {
  id: string;
  tenant_id: string;
  brand_id: string;
  brand?: Brand;
  name: string;
  slug: string;
  description: string | null;
  sku: string | null;
  category: string | null;
  price: number | null;
  currency: string;
  image_urls: string[] | null;
  features: string[] | null;
  benefits: string[] | null;
  attributes: Record<string, any> | null;
  is_active: boolean;
  metadata: Record<string, any> | null;
  created_at: string;
  updated_at: string;
}

export interface PaginatedResponse<T> {
  data: T[];
  meta: {
    total: number;
    page: number;
    limit: number;
    totalPages: number;
    hasNextPage: boolean;
    hasPreviousPage: boolean;
  };
}

export interface PaginationParams {
  page?: number;
  limit?: number;
  search?: string;
  sortBy?: string;
  sortOrder?: 'ASC' | 'DESC';
}

// Clients API
export const clientsApi = {
  getAll: (params?: PaginationParams) =>
    apiClient.get<PaginatedResponse<Client>>('/crm/clients', { params }),

  getById: (id: string) => apiClient.get<Client>(`/crm/clients/${id}`),

  create: (data: Partial<Client>) =>
    apiClient.post<Client>('/crm/clients', data),

  update: (id: string, data: Partial<Client>) =>
    apiClient.put<Client>(`/crm/clients/${id}`, data),

  delete: (id: string) => apiClient.delete(`/crm/clients/${id}`),
};

// Brands API
export const brandsApi = {
  getAll: (params?: PaginationParams & { clientId?: string }) =>
    apiClient.get<PaginatedResponse<Brand>>('/crm/brands', { params }),

  getById: (id: string) => apiClient.get<Brand>(`/crm/brands/${id}`),

  getByClient: (clientId: string) =>
    apiClient.get<Brand[]>(`/crm/brands/client/${clientId}`),

  create: (data: Partial<Brand>) =>
    apiClient.post<Brand>('/crm/brands', data),

  update: (id: string, data: Partial<Brand>) =>
    apiClient.put<Brand>(`/crm/brands/${id}`, data),

  delete: (id: string) => apiClient.delete(`/crm/brands/${id}`),
};

// Products API
export const productsApi = {
  getAll: (params?: PaginationParams & { brandId?: string }) =>
    apiClient.get<PaginatedResponse<Product>>('/crm/products', { params }),

  getById: (id: string) => apiClient.get<Product>(`/crm/products/${id}`),

  getByBrand: (brandId: string) =>
    apiClient.get<Product[]>(`/crm/products/brand/${brandId}`),

  create: (data: Partial<Product>) =>
    apiClient.post<Product>('/crm/products', data),

  update: (id: string, data: Partial<Product>) =>
    apiClient.put<Product>(`/crm/products/${id}`, data),

  delete: (id: string) => apiClient.delete(`/crm/products/${id}`),
};
