import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import { gql } from 'graphql-request';
import { fetchGraphQL, createQueryKey } from '../client';

// GraphQL Queries
const CUSTOMERS_QUERY = gql`
  query Customers($companyId: ID!) {
    customers(companyId: $companyId) {
      id
      companyId
      customerNumber
      customerType
      name
      cvr
      address
      postalCode
      city
      country
      email
      phone
      paymentTermsDays
      defaultRevenueAccountId
      subLedgerAccountId
      isActive
      createdAt
      updatedAt
    }
  }
`;

const ACTIVE_CUSTOMERS_QUERY = gql`
  query ActiveCustomers($companyId: ID!) {
    activeCustomers(companyId: $companyId) {
      id
      companyId
      customerNumber
      customerType
      name
      cvr
      address
      postalCode
      city
      country
      email
      phone
      paymentTermsDays
      defaultRevenueAccountId
      subLedgerAccountId
      isActive
      createdAt
      updatedAt
    }
  }
`;

const CUSTOMER_QUERY = gql`
  query Customer($id: ID!) {
    customer(id: $id) {
      id
      companyId
      customerNumber
      customerType
      name
      cvr
      address
      postalCode
      city
      country
      email
      phone
      paymentTermsDays
      defaultRevenueAccountId
      subLedgerAccountId
      isActive
      createdAt
      updatedAt
    }
  }
`;

// Types
export type CustomerType = 'Business' | 'Private';

export interface Customer {
  id: string;
  companyId: string;
  customerNumber: string;
  customerType: CustomerType;
  name: string;
  cvr?: string;
  address?: string;
  postalCode?: string;
  city?: string;
  country?: string;
  email?: string;
  phone?: string;
  paymentTermsDays: number;
  defaultRevenueAccountId?: string;
  subLedgerAccountId: string;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
}

// Response types
interface CustomersResponse {
  customers: Customer[];
}

interface ActiveCustomersResponse {
  activeCustomers: Customer[];
}

interface CustomerResponse {
  customer: Customer;
}

/**
 * Hook to fetch all customers for a company.
 */
export function useCustomers(
  companyId: string | undefined,
  options?: Omit<UseQueryOptions<Customer[], Error>, 'queryKey' | 'queryFn'>
) {
  return useQuery({
    queryKey: createQueryKey('customers', { companyId }),
    queryFn: async () => {
      if (!companyId) return [];
      const data = await fetchGraphQL<CustomersResponse>(CUSTOMERS_QUERY, { companyId });
      return data.customers;
    },
    enabled: !!companyId,
    ...options,
  });
}

/**
 * Hook to fetch active customers for a company.
 */
export function useActiveCustomers(
  companyId: string | undefined,
  options?: Omit<UseQueryOptions<Customer[], Error>, 'queryKey' | 'queryFn'>
) {
  return useQuery({
    queryKey: createQueryKey('activeCustomers', { companyId }),
    queryFn: async () => {
      if (!companyId) return [];
      const data = await fetchGraphQL<ActiveCustomersResponse>(ACTIVE_CUSTOMERS_QUERY, { companyId });
      return data.activeCustomers;
    },
    enabled: !!companyId,
    ...options,
  });
}

/**
 * Hook to fetch a single customer by ID.
 */
export function useCustomer(
  id: string | undefined,
  options?: Omit<UseQueryOptions<Customer | null, Error>, 'queryKey' | 'queryFn'>
) {
  return useQuery({
    queryKey: createQueryKey('customer', { id }),
    queryFn: async () => {
      if (!id) return null;
      const data = await fetchGraphQL<CustomerResponse>(CUSTOMER_QUERY, { id });
      return data.customer;
    },
    enabled: !!id,
    ...options,
  });
}

