import { api } from './client';
import type { ApiResponse } from '../../types';

// Tenant/Workshop Settings
export interface TenantSettings {
  id: string;
  name: string;
  legal_name: string;
  rfc: string;
  address: string;
  city: string;
  state: string;
  zip_code: string;
  phone: string;
  email: string;
  website: string;
  logo_url: string | null;
  default_tax_rate: number;
  labor_rate: number;
  working_hours_start: string;
  working_hours_end: string;
  quote_validity_days: number;
  currency: string;
  timezone: string;
  created_at: string;
  updated_at: string;
}

export interface UpdateTenantSettingsRequest {
  name?: string;
  legal_name?: string;
  rfc?: string;
  address?: string;
  city?: string;
  state?: string;
  zip_code?: string;
  phone?: string;
  email?: string;
  website?: string;
  logo_url?: string | null;
  default_tax_rate?: number;
  labor_rate?: number;
  working_hours_start?: string;
  working_hours_end?: string;
  quote_validity_days?: number;
}

// Work Bay Types
export interface WorkBay {
  id: string;
  tenant_id: string;
  name: string;
  bay_type: 'general' | 'diesel' | 'heavy_duty';
  status: 'available' | 'occupied' | 'maintenance';
  current_order_id: string | null;
  notes: string | null;
  created_at: string;
  updated_at: string;
}

export interface CreateWorkBayRequest {
  name: string;
  bay_type: 'general' | 'diesel' | 'heavy_duty';
  notes?: string;
}

export interface UpdateWorkBayRequest {
  name?: string;
  bay_type?: 'general' | 'diesel' | 'heavy_duty';
  status?: 'available' | 'occupied' | 'maintenance';
  notes?: string;
}

// Service Catalog
export interface ServiceCatalogItem {
  id: string;
  tenant_id: string;
  code: string;
  name: string;
  description: string | null;
  category: string;
  default_price: number;
  estimated_hours: number;
  is_active: boolean;
  created_at: string;
  updated_at: string;
}

export interface CreateServiceCatalogItemRequest {
  code: string;
  name: string;
  description?: string;
  category: string;
  default_price: number;
  estimated_hours: number;
}

// API
export const settingsApi = {
  // Get current tenant settings
  getSettings: () =>
    api.get<ApiResponse<TenantSettings>>('/settings'),

  // Update tenant settings
  updateSettings: (data: UpdateTenantSettingsRequest) =>
    api.patch<ApiResponse<TenantSettings>>('/settings', data),

  // Upload logo
  uploadLogo: (file: File) => {
    const formData = new FormData();
    formData.append('logo', file);
    return api.post<ApiResponse<{ logo_url: string }>>('/settings/logo', formData);
  },

  // Work Bays
  listBays: () =>
    api.get<ApiResponse<WorkBay[]>>('/settings/bays'),

  createBay: (data: CreateWorkBayRequest) =>
    api.post<ApiResponse<WorkBay>>('/settings/bays', data),

  updateBay: (id: string, data: UpdateWorkBayRequest) =>
    api.patch<ApiResponse<WorkBay>>(`/settings/bays/${id}`, data),

  deleteBay: (id: string) =>
    api.delete<ApiResponse<null>>(`/settings/bays/${id}`),

  // Service Catalog
  listServices: () =>
    api.get<ApiResponse<ServiceCatalogItem[]>>('/settings/services'),

  createService: (data: CreateServiceCatalogItemRequest) =>
    api.post<ApiResponse<ServiceCatalogItem>>('/settings/services', data),

  updateService: (id: string, data: Partial<CreateServiceCatalogItemRequest>) =>
    api.patch<ApiResponse<ServiceCatalogItem>>(`/settings/services/${id}`, data),

  deleteService: (id: string) =>
    api.delete<ApiResponse<null>>(`/settings/services/${id}`),

  toggleServiceActive: (id: string, isActive: boolean) =>
    api.patch<ApiResponse<ServiceCatalogItem>>(`/settings/services/${id}`, { is_active: isActive }),
};
