// Copyright 2026 CRMy Contributors
// SPDX-License-Identifier: Apache-2.0

import { useState, useMemo, useEffect } from 'react';
import { useNavigate } from 'react-router-dom';
import { TopBar } from '@/components/layout/TopBar';
import { OnboardingEmptyState } from '@/components/crm/OnboardingEmptyState';
import { useOpportunities } from '@/api/hooks';
import { useAppStore } from '@/store/appStore';
import { useAgentSettings } from '@/contexts/AgentSettingsContext';
import { StageBadge, DealHealthBadge } from '@/components/crm/CrmWidgets';
import { ListToolbar, type FilterConfig, type SortOption } from '@/components/crm/ListToolbar';
import { RecordMemoryIndicator } from '@/components/crm/RecordMemoryIndicator';
import { DatePicker } from '@/components/ui/date-picker';
import { motion } from 'framer-motion';
import { Columns3, List, BarChart3, Plus, Bot, ChevronUp, ChevronDown, Briefcase, FileText } from 'lucide-react';
import { PaginationBar } from '@/components/crm/PaginationBar';
import { ContactAvatar } from '@/components/crm/ContactAvatar';
import { useRecordMemoryCounts } from '@/hooks/useRecordMemoryCounts';
import { stageConfig } from '@/lib/stageConfig';
import { headerDescription } from '@/lib/headerCopy';
import { formatCompactCurrency } from '@/lib/utils';

type ViewMode = 'kanban' | 'table' | 'forecast';
const kanbanStages = ['prospecting', 'qualification', 'proposal', 'negotiation', 'closed_won', 'closed_lost'];

type CloseDatePreset = 'all' | 'today' | 'this_week' | 'this_month' | 'this_quarter' | 'custom';

const CLOSE_DATE_OPTIONS: { value: CloseDatePreset; label: string }[] = [
  { value: 'all', label: 'All' },
  { value: 'today', label: 'Today' },
  { value: 'this_week', label: 'This Week' },
  { value: 'this_month', label: 'This Month' },
  { value: 'this_quarter', label: 'This Quarter' },
  { value: 'custom', label: 'Custom' },
];

function getCloseDateRange(preset: CloseDatePreset): { start: Date; end: Date } | null {
  if (preset === 'all') return null;
  const now = new Date();
  if (preset === 'today') {
    const start = new Date(now); start.setHours(0, 0, 0, 0);
    const end = new Date(now); end.setHours(23, 59, 59, 999);
    return { start, end };
  }
  if (preset === 'this_week') {
    const start = new Date(now);
    const day = start.getDay();
    start.setDate(start.getDate() + (day === 0 ? -6 : 1 - day));
    start.setHours(0, 0, 0, 0);
    const end = new Date(start); end.setDate(start.getDate() + 6); end.setHours(23, 59, 59, 999);
    return { start, end };
  }
  if (preset === 'this_month') {
    const start = new Date(now.getFullYear(), now.getMonth(), 1);
    const end = new Date(now.getFullYear(), now.getMonth() + 1, 0, 23, 59, 59, 999);
    return { start, end };
  }
  if (preset === 'this_quarter') {
    const q = Math.floor(now.getMonth() / 3);
    const start = new Date(now.getFullYear(), q * 3, 1);
    const end = new Date(now.getFullYear(), q * 3 + 3, 0, 23, 59, 59, 999);
    return { start, end };
  }
  return null; // custom — handled separately
}

const filterConfigs: FilterConfig[] = [
  { key: 'stage', label: 'Stage', options: kanbanStages.map(k => ({ value: k, label: stageConfig[k]?.label ?? k })) },
];
const sortOptions: SortOption[] = [
  { key: 'name', label: 'Opportunity Name' }, { key: 'amount', label: 'Amount' },
  { key: 'stage', label: 'Stage' }, { key: 'probability', label: 'Probability' },
  { key: 'deal_health_score', label: 'Health Score' },
  { key: 'close_date', label: 'Close Date' },
  { key: 'created_at', label: 'Created' },
];

// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Opportunity = any;

function opportunityAmount(opportunity: Opportunity): number {
  const amount = Number(opportunity.amount ?? 0);
  return Number.isFinite(amount) ? amount : 0;
}

function opportunityProbability(opportunity: Opportunity): number {
  const probability = Number(opportunity.probability ?? 0);
  if (!Number.isFinite(probability)) return 0;
  return Math.min(100, Math.max(0, probability));
}

function isOpenOpportunity(opportunity: Opportunity): boolean {
  return opportunity.stage !== 'closed_won' && opportunity.stage !== 'closed_lost';
}

function isBestCaseOpportunity(opportunity: Opportunity): boolean {
  return isOpenOpportunity(opportunity) && ['best_case', 'commit'].includes(String(opportunity.forecast_cat ?? 'pipeline'));
}

export default function Opportunities() {
  const navigate = useNavigate();
  const [view, setView] = useState<ViewMode>('table');
  const { openDrawer, openQuickAdd, openAIWithContext, openDrawerBriefing } = useAppStore();
  const { enabled: agentEnabled } = useAgentSettings();
  const [search, setSearch] = useState('');
  const [activeFilters, setActiveFilters] = useState<Record<string, string[]>>({});
  const [sort, setSort] = useState<{ key: string; dir: 'asc' | 'desc' } | null>(null);
  const [closeDate, setCloseDate] = useState<CloseDatePreset>('this_quarter');
  const [page, setPage] = useState(1);
  const [pageSize, setPageSize] = useState(25);
  const [customFrom, setCustomFrom] = useState('');
  const [customTo, setCustomTo] = useState('');

  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  const { data, isLoading } = useOpportunities({ q: search || undefined, limit: 200 }) as any;
  const allOpportunities: Opportunity[] = data?.data ?? [];
  const memoryCounts = useRecordMemoryCounts('opportunity');

  const handleFilterChange = (key: string, values: string[]) => {
    setActiveFilters(prev => { const next = { ...prev }; if (values.length === 0) delete next[key]; else next[key] = values; return next; });
  };
  const handleSortChange = (key: string) => {
    setSort(prev => prev?.key === key ? { key, dir: prev.dir === 'asc' ? 'desc' : 'asc' } : { key, dir: 'asc' });
  };

  const filtered = useMemo(() => {
    let result = [...allOpportunities];
    if (activeFilters.stage?.length) result = result.filter(d => activeFilters.stage.includes(d.stage as string));

    // Close date filtering
    if (closeDate !== 'all') {
      let start: Date, end: Date;
      if (closeDate === 'custom') {
        start = customFrom ? new Date(customFrom + 'T00:00:00') : new Date(0);
        end = customTo ? new Date(customTo + 'T23:59:59') : new Date(8640000000000000);
      } else {
        const range = getCloseDateRange(closeDate);
        if (range) { start = range.start; end = range.end; }
        else { start = new Date(0); end = new Date(8640000000000000); }
      }
      result = result.filter(d => {
        if (!d.close_date) return false;
        const cd = new Date(d.close_date);
        return cd >= start && cd <= end;
      });
    }

    if (sort) {
      result.sort((a, b) => {
        const aVal = (a[sort.key] ?? '') as string | number;
        const bVal = (b[sort.key] ?? '') as string | number;
        if (typeof aVal === 'number' && typeof bVal === 'number') return sort.dir === 'asc' ? aVal - bVal : bVal - aVal;
        return sort.dir === 'asc' ? String(aVal).localeCompare(String(bVal)) : String(bVal).localeCompare(String(aVal));
      });
    }
    return result;
  }, [allOpportunities, activeFilters, sort, closeDate, customFrom, customTo]);

  useEffect(() => { setPage(1); }, [search, activeFilters, sort, closeDate, customFrom, customTo]);
  const paginated = filtered.slice((page - 1) * pageSize, page * pageSize);
  const forecastMetrics = useMemo(() => {
    const open = filtered.filter(isOpenOpportunity);
    return {
      weightedPipeline: open.reduce((sum, opportunity) => sum + opportunityAmount(opportunity) * opportunityProbability(opportunity) / 100, 0),
      bestCase: filtered.filter(isBestCaseOpportunity).reduce((sum, opportunity) => sum + opportunityAmount(opportunity), 0),
      closedWon: filtered.filter(opportunity => opportunity.stage === 'closed_won').reduce((sum, opportunity) => sum + opportunityAmount(opportunity), 0),
    };
  }, [filtered]);

  const SortHeader = ({ label, sortKey }: { label: string; sortKey: string }) => (
    <th onClick={() => handleSortChange(sortKey)} className="text-left px-4 py-3 text-xs font-display font-semibold text-muted-foreground cursor-pointer hover:text-foreground transition-colors select-none">
      <span className="inline-flex items-center gap-1">
        {label}
        {sort?.key === sortKey && (sort.dir === 'asc' ? <ChevronUp className="w-3 h-3" /> : <ChevronDown className="w-3 h-3" />)}
      </span>
    </th>
  );

  return (
    <div className="flex flex-col h-full">
      <TopBar
        title="Opportunities"
        icon={Briefcase}
        iconClassName="text-accent"
        description={headerDescription('Track deals and pipeline stages', filtered.length, 'opportunity', 'opportunities')}
      >
        <div className="hidden h-9 rounded-xl border border-border bg-muted p-0.5 md:inline-flex md:mr-2">
          {[
            { mode: 'kanban', icon: Columns3 },
            { mode: 'table', icon: List },
            { mode: 'forecast', icon: BarChart3 },
          ].map(({ mode, icon: Icon }) => (
            <button key={mode} onClick={() => setView(mode as ViewMode)}
              className={`p-1.5 rounded-lg text-sm transition-all ${view === mode ? 'bg-card text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground'}`}>
              <Icon className="w-4 h-4" />
            </button>
          ))}
        </div>
      </TopBar>

      {/* Close date selector */}
      <div className="px-4 md:px-6 pt-3 pb-1 flex flex-wrap items-center gap-2">
        <span className="text-xs text-muted-foreground font-semibold">Close Date:</span>
        <div className="inline-flex rounded-xl border border-border bg-muted/40 p-0.5 gap-0.5">
          {CLOSE_DATE_OPTIONS.map(opt => (
            <button key={opt.value} onClick={() => setCloseDate(opt.value)}
              className={['px-3 py-1.5 text-xs font-medium rounded-lg transition-all',
                closeDate === opt.value ? 'bg-background text-foreground shadow-sm' : 'text-muted-foreground hover:text-foreground',
              ].join(' ')}>
              {opt.label}
            </button>
          ))}
        </div>
        {closeDate === 'custom' && (
          <div className="flex items-center gap-2">
            <DatePicker
              value={customFrom}
              onChange={setCustomFrom}
              size="sm"
              placeholder="From"
              className="w-36"
            />
            <span className="text-xs text-muted-foreground">to</span>
            <DatePicker
              value={customTo}
              onChange={setCustomTo}
              size="sm"
              placeholder="To"
              className="w-36"
            />
          </div>
        )}
      </div>

      <ListToolbar searchValue={search} onSearchChange={setSearch} searchPlaceholder="Search opportunities..."
        filters={filterConfigs} activeFilters={activeFilters} onFilterChange={handleFilterChange}
        onClearFilters={() => setActiveFilters({})} sortOptions={sortOptions} currentSort={sort}
        onSortChange={handleSortChange} entityType="opportunities"
        onAdd={() => openQuickAdd('opportunity')} addLabel="New Opportunity" />

      <div className="flex-1 overflow-y-auto pb-24 md:pb-6">
        {isLoading ? (
          <div className="flex gap-4 px-4 md:px-6 pt-2">
            {[...Array(4)].map((_, i) => <div key={i} className="flex-shrink-0 w-72 h-64 bg-muted/50 rounded-2xl animate-pulse" />)}
          </div>
        ) : view === 'kanban' ? (
          <>
            <div className="border-t border-border mb-4" />
          <div className="flex gap-3 md:gap-4 px-4 md:px-6 pb-4 overflow-x-auto min-h-full snap-x snap-mandatory md:snap-none no-scrollbar">
            {kanbanStages.map((stage) => {
              const config = stageConfig[stage] ?? { label: stage, color: '#94a3b8' };
              const stageOpps = filtered.filter((d) => d.stage === stage);
              const total = stageOpps.reduce((sum, d) => sum + opportunityAmount(d), 0);
              return (
                <div key={stage} className="flex-shrink-0 w-[280px] md:w-72 flex flex-col snap-center">
                  <div className="flex items-center justify-between mb-3 px-1">
                    <div className="flex items-center gap-2">
                      <span className="px-2.5 py-1 rounded-lg text-xs font-semibold" style={{ backgroundColor: config.color + '20', color: config.color }}>
                        {config.label}
                      </span>
                      <span className="text-xs text-muted-foreground font-mono">{stageOpps.length}</span>
                    </div>
                    <span className="text-xs text-muted-foreground font-mono">{formatCompactCurrency(total)}</span>
                  </div>
                  <div className="flex-1 space-y-2">
                    {stageOpps.map((opp, i) => {
                      const contactName = (opp.contact_name ?? opp.contactName ?? '') as string;
                      const amount = opportunityAmount(opp);
                      const daysInStage = (opp.days_in_stage ?? opp.daysInStage ?? 0) as number;
                      return (
                        <motion.div key={opp.id as string} initial={{ opacity: 0, y: 8 }} animate={{ opacity: 1, y: 0 }} transition={{ delay: i * 0.03 }}
                          onClick={() => openDrawer('opportunity', opp.id as string)}
                          className="bg-card border border-border rounded-2xl p-3.5 cursor-pointer hover:border-primary/30 hover:shadow-md transition-all press-scale group">
                          <div className="flex items-start justify-between">
                            <div className="flex min-w-0 items-center gap-2">
                              <p className="truncate text-sm font-display font-bold text-foreground">{opp.name as string}</p>
                              <RecordMemoryIndicator count={memoryCounts.get(opp.id as string)} className="shrink-0" />
                            </div>
                            <div className="flex items-center gap-0.5">
                              <button onClick={(e) => { e.stopPropagation(); openDrawerBriefing('opportunity', opp.id as string); }}
                                className="p-0.5 rounded-lg hover:bg-primary/10 transition-all" title="View briefing">
                                <FileText className="w-3.5 h-3.5 text-primary" />
                              </button>
                              {agentEnabled && (
                                <button onClick={(e) => { e.stopPropagation(); openAIWithContext({ type: 'opportunity', id: opp.id as string, name: opp.name as string, detail: formatCompactCurrency(amount) }); navigate('/agent'); }}
                                  className="p-0.5 rounded-lg hover:bg-violet-500/10 transition-all" title="Open with agent">
                                  <Bot className="w-3.5 h-3.5 text-violet-500" />
                                </button>
                              )}
                            </div>
                          </div>
                          {contactName && (
                            <div className="flex items-center gap-2 mt-2">
                              <ContactAvatar name={contactName} className="w-5 h-5 rounded-full text-[8px]" />
                              <span className="text-xs text-muted-foreground">{contactName}</span>
                            </div>
                          )}
                          <div className="flex items-center justify-between mt-2.5">
                            <span className="text-sm font-display font-extrabold text-foreground">
                              {formatCompactCurrency(amount)}
                            </span>
                            <div className="flex items-center gap-1.5">
                              {opp.deal_health_score != null && (
                                <DealHealthBadge score={opp.deal_health_score as number} />
                              )}
                              {daysInStage > 0 && (
                                daysInStage > 14 ? (
                                  <span className="px-2 py-0.5 rounded-lg text-xs bg-destructive/15 text-destructive font-semibold">{daysInStage}d</span>
                                ) : (
                                  <span className="text-xs text-muted-foreground">{daysInStage}d</span>
                                )
                              )}
                            </div>
                          </div>
                        </motion.div>
                      );
                    })}
                    <button onClick={() => openQuickAdd('opportunity')}
                      className="w-full flex items-center justify-center gap-1 py-2.5 text-xs text-muted-foreground hover:text-foreground hover:bg-muted/50 rounded-xl transition-colors press-scale">
                      <Plus className="w-3.5 h-3.5" /> Add Opportunity
                    </button>
                  </div>
                </div>
              );
            })}
          </div>
          </>
        ) : view === 'table' ? (
          <div className="px-4 md:px-6">
            {filtered.length === 0 ? (
              allOpportunities.length === 0 && !search && Object.keys(activeFilters).length === 0 ? (
                <OnboardingEmptyState
                  icon={Briefcase}
                  title="No opportunities yet"
                  description="Opportunities track stage, value, context, and next steps."
                  showSampleData={false}
                  iconClassName="text-accent"
                  iconBgClassName="bg-accent/15"
                />
              ) : (
                <OnboardingEmptyState
                  icon={Briefcase}
                  title="No opportunities match"
                  description="Adjust filters or date range to find the deal you need."
                  primary={{ label: 'Clear filters', onClick: () => { setSearch(''); setActiveFilters({}); setCloseDate('all'); } }}
                  showSampleData={false}
                  iconClassName="text-accent"
                  iconBgClassName="bg-accent/15"
                />
              )
            ) : (
              <div className="bg-card border border-border rounded-2xl overflow-hidden shadow-sm">
                <div className="overflow-x-auto">
                  <table className="w-full text-sm">
                    <thead>
                      <tr className="border-b border-border bg-surface-sunken/50">
                        <SortHeader label="Opportunity" sortKey="name" />
                        <th className="text-left px-4 py-3 text-xs font-display font-semibold text-muted-foreground">Account</th>
                        <th className="text-left px-4 py-3 text-xs font-display font-semibold text-muted-foreground">Contact</th>
                        <SortHeader label="Amount" sortKey="amount" />
                        <SortHeader label="Stage" sortKey="stage" />
                        <SortHeader label="Probability" sortKey="probability" />
                        <SortHeader label="Health" sortKey="deal_health_score" />
                        <SortHeader label="Close Date" sortKey="close_date" />
                        <th className="px-2 py-3 w-16"></th>
                      </tr>
                    </thead>
                    <tbody>
                      {paginated.map((d, i) => {
                        const contactName = (d.contact_name ?? d.contactName ?? '') as string;
                        const accountName = (d.account_name ?? d.accountName ?? '') as string;
                        const amount = opportunityAmount(d);
                        return (
                          <tr key={d.id as string} onClick={() => openDrawer('opportunity', d.id as string)}
                            className={`border-b border-border last:border-0 hover:bg-primary/5 cursor-pointer transition-colors group ${i % 2 === 1 ? 'bg-surface-sunken/30' : ''}`}>
                            <td className="px-4 py-3">
                              <div className="flex items-center gap-2">
                                <span className="font-display font-bold text-foreground">{d.name as string}</span>
                                <RecordMemoryIndicator count={memoryCounts.get(d.id as string)} />
                              </div>
                            </td>
                            <td className="px-4 py-3 text-muted-foreground">{accountName || '—'}</td>
                            <td className="px-4 py-3">
                              {contactName ? (
                                <span className="text-muted-foreground">{contactName}</span>
                              ) : (
                                <span className="text-muted-foreground">—</span>
                              )}
                            </td>
                            <td className="px-4 py-3 font-display font-bold text-foreground">
                              {formatCompactCurrency(amount)}
                            </td>
                            <td className="px-4 py-3">{d.stage && <StageBadge stage={d.stage as string} />}</td>
                            <td className="px-4 py-3 text-muted-foreground">{d.probability ? `${d.probability}%` : '—'}</td>
                            <td className="px-4 py-3">
                              {d.deal_health_score != null
                                ? <DealHealthBadge score={d.deal_health_score as number} />
                                : <span className="text-muted-foreground text-xs">—</span>}
                            </td>
                            <td className="px-4 py-3 text-muted-foreground text-xs">
                              {d.close_date ? new Date(d.close_date as string).toLocaleDateString() : '—'}
                            </td>
                            <td className="px-2 py-3">
                              <div className="flex items-center gap-0.5 opacity-0 transition-all group-hover:opacity-100">
                                <button
                                  type="button"
                                  onClick={(e) => { e.stopPropagation(); openDrawerBriefing('opportunity', d.id as string); }}
                                  className="p-1.5 rounded-lg hover:bg-primary/10 transition-colors"
                                  title="View briefing"
                                >
                                  <FileText className="h-3.5 w-3.5 text-primary" />
                                </button>
                                {agentEnabled && (
                                <button onClick={(e) => { e.stopPropagation(); openAIWithContext({ type: 'opportunity', id: d.id as string, name: d.name as string, detail: formatCompactCurrency(amount) }); navigate('/agent'); }}
                                  className="p-1.5 rounded-lg hover:bg-violet-500/10 transition-all" title="Open with agent">
                                  <Bot className="w-3.5 h-3.5 text-violet-500" />
                                </button>
                                )}
                              </div>
                            </td>
                          </tr>
                        );
                      })}
                    </tbody>
                  </table>
                </div>
                <div className="px-4">
                  <PaginationBar page={page} pageSize={pageSize} total={filtered.length} onPageChange={setPage} onPageSizeChange={setPageSize} />
                </div>
              </div>
            )}
          </div>
        ) : (
          <div className="px-4 md:px-6">
            <div className="grid grid-cols-1 md:grid-cols-3 gap-3 mb-6">
              {[
                { label: 'Weighted Pipeline', value: forecastMetrics.weightedPipeline },
                { label: 'Best Case', value: forecastMetrics.bestCase },
                { label: 'Closed Won', value: forecastMetrics.closedWon },
              ].map((card) => (
                <div key={card.label} className="bg-card border border-border rounded-2xl p-5 shadow-sm">
                  <p className="text-xs text-muted-foreground font-display font-semibold">{card.label}</p>
                  <p className="text-2xl font-display font-extrabold text-foreground mt-1">
                    {formatCompactCurrency(card.value)}
                  </p>
                </div>
              ))}
            </div>
            <div className="bg-card border border-border rounded-2xl p-5 shadow-sm">
              <h3 className="font-display font-bold text-foreground mb-4">Pipeline by stage</h3>
              <div className="space-y-4">
                {kanbanStages.filter(s => s !== 'closed_lost').map((stage) => {
                  const config = stageConfig[stage] ?? { label: stage, color: '#94a3b8' };
                  const total = filtered.filter(d => d.stage === stage).reduce((sum, d) => sum + opportunityAmount(d), 0);
                  const max = Math.max(...kanbanStages.map(s => filtered.filter(d => d.stage === s).reduce((sum, d) => sum + opportunityAmount(d), 0)), 1);
                  return (
                    <div key={stage} className="flex items-center gap-3">
                      <span className="text-xs w-28 truncate font-semibold" style={{ color: config.color }}>{config.label}</span>
                      <div className="flex-1 h-8 bg-muted rounded-xl overflow-hidden">
                        <motion.div initial={{ width: 0 }} animate={{ width: `${(total / max) * 100}%` }}
                          transition={{ duration: 0.8, ease: 'easeOut' }}
                          className="h-full rounded-xl flex items-center px-3"
                          style={{ backgroundColor: config.color + '30' }}>
                          <span className="text-xs font-display font-bold" style={{ color: config.color }}>
                            {formatCompactCurrency(total)}
                          </span>
                        </motion.div>
                      </div>
                    </div>
                  );
                })}
              </div>
            </div>
          </div>
        )}
      </div>
    </div>
  );
}
