import { createSelector } from '@reduxjs/toolkit';
import type { RootState } from '@/store/store';
import type { SalesforceCrmState } from './salesforceCrmSlice';

// Base selector
const selectSalesforceCrmState = (state: RootState): SalesforceCrmState => 
  state.salesforceCrm as SalesforceCrmState;

// Data selectors
export const selectSalesforceEvents = createSelector(
  [selectSalesforceCrmState],
  (state) => state.events
);

export const selectSalesforceLeads = createSelector(
  [selectSalesforceCrmState],
  (state) => state.leads
);

export const selectSalesforceTasks = createSelector(
  [selectSalesforceCrmState],
  (state) => state.tasks
);

export const selectSalesforceAccounts = createSelector(
  [selectSalesforceCrmState],
  (state) => state.accounts
);

export const selectSalesforceOpportunities = createSelector(
  [selectSalesforceCrmState],
  (state) => state.opportunities
);

// Loading selectors
export const selectSalesforceCrmLoading = createSelector(
  [selectSalesforceCrmState],
  (state) => state.loading
);

export const selectIsAnyLoading = createSelector(
  [selectSalesforceCrmLoading],
  (loading) => Object.values(loading).some(isLoading => isLoading)
);

// Error selectors
export const selectSalesforceCrmErrors = createSelector(
  [selectSalesforceCrmState],
  (state) => state.errors
);

export const selectHasAnyError = createSelector(
  [selectSalesforceCrmErrors],
  (errors) => Object.values(errors).some(error => error !== null)
);

// Pagination selectors
export const selectEventsPagination = createSelector(
  [selectSalesforceCrmState],
  (state) => state.pagination.events
);

export const selectLeadsPagination = createSelector(
  [selectSalesforceCrmState],
  (state) => state.pagination.leads
);

export const selectTasksPagination = createSelector(
  [selectSalesforceCrmState],
  (state) => state.pagination.tasks
);

export const selectAccountsPagination = createSelector(
  [selectSalesforceCrmState],
  (state) => state.pagination.accounts
);

export const selectOpportunitiesPagination = createSelector(
  [selectSalesforceCrmState],
  (state) => state.pagination.opportunities
);

// Count selectors
export const selectSalesforceCrmCounts = createSelector(
  [selectSalesforceCrmState],
  (state) => state.counts
);

// Stats selectors
export const selectSalesforceCrmStats = createSelector(
  [selectSalesforceCrmState],
  (state) => state.stats
);

// Dashboard data selector - combines all data for dashboard view
export const selectDashboardData = createSelector(
  [
    selectSalesforceEvents,
    selectSalesforceLeads,
    selectSalesforceTasks,
    selectSalesforceAccounts,
    selectSalesforceOpportunities,
    selectSalesforceCrmCounts,
  ],
  (events, leads, tasks, accounts, opportunities, counts) => ({
    events,
    leads,
    tasks,
    accounts,
    opportunities,
    counts,
    summary: {
      totalEvents: events.length,
      totalLeads: leads.length,
      totalTasks: tasks.length,
      totalAccounts: accounts.length,
      totalOpportunities: opportunities.length,
      
      // Event stats
      upcomingEvents: events.filter(e => e.StartDateTime && new Date(e.StartDateTime) > new Date()).length,
      
      // Lead stats
      newLeads: leads.filter(l => l.Status === 'New').length,
      workingLeads: leads.filter(l => l.Status === 'Working').length,
      qualifiedLeads: leads.filter(l => l.Status === 'Qualified').length,
      
      // Task stats
      openTasks: tasks.filter(t => t.Status === 'Open').length,
      completedTasks: tasks.filter(t => t.Status === 'Completed').length,
      highPriorityTasks: tasks.filter(t => t.Priority === 'High').length,
      
      // Opportunity stats
      openOpportunities: opportunities.filter(o => !o.IsClosed).length,
      totalPipelineValue: opportunities
        .filter(o => !o.IsClosed)
        .reduce((sum, o) => sum + (o.Amount || 0), 0),
      averageDealSize: opportunities.length > 0 
        ? opportunities.reduce((sum, o) => sum + (o.Amount || 0), 0) / opportunities.length
        : 0,
      wonOpportunities: opportunities.filter(o => o.IsWon).length,
    },
  })
);

// Last updated selector
export const selectLastUpdated = createSelector(
  [selectSalesforceCrmState],
  (state) => state.lastUpdated
);

