/**
 * System settings, currency, tax, and sequence hooks.
 */

import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { apiGet, apiPost, apiPatch, apiDelete, apiClient } from './client';

export interface Currency {
  id: string;
  code: string;
  name: string;
  symbol: string;
  is_default: boolean;
  created_at?: string;
  updated_at?: string;
}

export interface TaxRate {
  id: string;
  name: string;
  rate: number;
  is_default: boolean;
  country?: string | null;
  created_at?: string;
  updated_at?: string;
}

export interface Sequence {
  id: string;
  name: string;
  prefix: string;
  next_number: number;
  padding: number;
  created_at?: string;
  updated_at?: string;
}

export interface SystemSettings {
  id?: string;
  company_name: string;
  company_legal_form?: string | null;
  company_street: string;
  company_city: string;
  company_zip: string;
  company_country: string;
  tax_number?: string | null;
  vat_id?: string | null;
  iban?: string | null;
  bic?: string | null;
  bank_name?: string | null;
  ceo?: string | null;
  trade_register?: string | null;
  default_currency_id?: string | null;
  default_tax_id?: string | null;
  invoice_prefix: string;
  quote_prefix: string;
  payment_terms_days: number;
  // Theme customization
  theme_primary_color?: string;
  theme_accent_color?: string;
  theme_font_family?: string;
  theme_border_radius?: string;
  created_at?: string;
  updated_at?: string;
}

// System Settings
export function useSystemSettings() {
  return useQuery({
    queryKey: ['systemSettings'],
    queryFn: () => apiGet<SystemSettings>('/system-settings'),
  });
}

export function useUpdateSystemSettings() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (data: Partial<SystemSettings>) => apiClient.put('/system-settings', data).then(r => r.data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['systemSettings'] });
    },
  });
}

// Currencies
export function useCurrencies() {
  return useQuery({
    queryKey: ['currencies'],
    queryFn: () => apiGet<{ items: Currency[]; total: number }>('/currencies'),
  });
}

export function useCreateCurrency() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (data: Partial<Currency>) => apiPost('/currencies', data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['currencies'] });
    },
  });
}

export function useUpdateCurrency() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, data }: { id: string; data: Partial<Currency> }) =>
      apiPatch(`/currencies/${id}`, data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['currencies'] });
    },
  });
}

export function useDeleteCurrency() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: string) => apiDelete(`/currencies/${id}`),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['currencies'] });
    },
  });
}

// Taxes
export function useTaxes() {
  return useQuery({
    queryKey: ['taxes'],
    queryFn: () => apiGet<{ items: TaxRate[]; total: number }>('/taxes'),
  });
}

export function useCreateTax() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (data: Partial<TaxRate>) => apiPost('/taxes', data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['taxes'] });
    },
  });
}

export function useUpdateTax() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, data }: { id: string; data: Partial<TaxRate> }) =>
      apiPatch(`/taxes/${id}`, data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['taxes'] });
    },
  });
}

export function useDeleteTax() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: string) => apiDelete(`/taxes/${id}`),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['taxes'] });
    },
  });
}

// Sequences
export function useSequences() {
  return useQuery({
    queryKey: ['sequences'],
    queryFn: () => apiGet<{ items: Sequence[]; total: number }>('/sequences'),
  });
}

export function useCreateSequence() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (data: Partial<Sequence>) => apiPost('/sequences', data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['sequences'] });
    },
  });
}

export function useUpdateSequence() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: ({ id, data }: { id: string; data: Partial<Sequence> }) =>
      apiPatch(`/sequences/${id}`, data),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['sequences'] });
    },
  });
}

export function useDeleteSequence() {
  const queryClient = useQueryClient();
  return useMutation({
    mutationFn: (id: string) => apiDelete(`/sequences/${id}`),
    onSuccess: () => {
      queryClient.invalidateQueries({ queryKey: ['sequences'] });
    },
  });
}
