import { createSelector } from '@reduxjs/toolkit';
import type { RootState } from '@/store/store';
import type { ZohoBooksContact, ZohoBooksInvoice } from '../types/ZohoBooksTypes';

// Base selectors
export const selectZohoBooksState = (state: RootState) => state.zohoBooks;

export const selectZohoBooksLoading = createSelector(
  [selectZohoBooksState],
  (state) => state.loading
);

export const selectZohoBooksError = createSelector(
  [selectZohoBooksState],
  (state) => state.error
);

export const selectZohoBooksLastUpdated = createSelector(
  [selectZohoBooksState],
  (state) => state.lastUpdated
);

// Data selectors
export const selectZohoBooksContacts = createSelector(
  [selectZohoBooksState],
  (state) => state.contacts
);

export const selectZohoBooksInvoices = createSelector(
  [selectZohoBooksState],
  (state) => state.invoices
);

export const selectZohoBooksExpenses = createSelector(
  [selectZohoBooksState],
  (state) => state.expenses
);

export const selectZohoBooksSalesOrders = createSelector(
  [selectZohoBooksState],
  (state) => state.salesOrders
);

export const selectZohoBooksPurchaseOrders = createSelector(
  [selectZohoBooksState],
  (state) => state.purchaseOrders
);

export const selectZohoBooksBills = createSelector(
  [selectZohoBooksState],
  (state) => state.bills
);

export const selectZohoBooksKPIData = createSelector(
  [selectZohoBooksState],
  (state) => state.kpiData
);

// Computed selectors
export const selectCustomers = createSelector(
  [selectZohoBooksContacts],
  (contacts) => contacts.filter((contact: ZohoBooksContact) => contact.contact_type === 'customer')
);

export const selectVendors = createSelector(
  [selectZohoBooksContacts],
  (contacts) => contacts.filter((contact: ZohoBooksContact) => contact.contact_type === 'vendor')
);

export const selectActiveInvoices = createSelector(
  [selectZohoBooksInvoices],
  (invoices) => invoices.filter((invoice: ZohoBooksInvoice) => 
    invoice.status === 'sent' || invoice.status === 'overdue'
  )
);

export const selectPaidInvoices = createSelector(
  [selectZohoBooksInvoices],
  (invoices) => invoices.filter((invoice: ZohoBooksInvoice) => invoice.status === 'paid')
);

export const selectDraftInvoices = createSelector(
  [selectZohoBooksInvoices],
  (invoices) => invoices.filter((invoice: ZohoBooksInvoice) => invoice.status === 'draft')
);

// Top customers by outstanding amount
export const selectTopCustomersByOutstanding = createSelector(
  [selectCustomers],
  (customers) => 
    customers
      .sort((a, b) => b.outstanding_receivable_amount - a.outstanding_receivable_amount)
      .slice(0, 5)
);

// Top vendors by outstanding amount
export const selectTopVendorsByOutstanding = createSelector(
  [selectVendors],
  (vendors) => 
    vendors
      .sort((a, b) => b.outstanding_payable_amount - a.outstanding_payable_amount)
      .slice(0, 5)
);

// Invoice status distribution
export const selectInvoiceStatusDistribution = createSelector(
  [selectZohoBooksInvoices],
  (invoices) => {
    const distribution = invoices.reduce((acc, invoice) => {
      acc[invoice.status] = (acc[invoice.status] || 0) + 1;
      return acc;
    }, {} as Record<string, number>);

    return Object.entries(distribution).map(([status, count]) => ({
      label: status.charAt(0).toUpperCase() + status.slice(1),
      value: count,
      color: getStatusColor(status),
    }));
  }
);

// Helper function to get status colors
const getStatusColor = (status: string): string => {
  const colors: Record<string, string> = {
    draft: '#94A3B8',
    sent: '#3AA0FF',
    invoiced: '#6366F1',
    paid: '#10B981',
    overdue: '#EF4444',
    void: '#6B7280',
  };
  return colors[status] || '#6B7280';
};
