import { Component, OnInit, OnDestroy } from '@angular/core';
import { DashboardFilterService } from '../../services/dashboard-filter.service';
import { Subscription } from 'rxjs';

interface DealDetail {
  id: number;
  companyName: string;
  stage: string;
  description: string;
  amount: number;
  stageDays: number;
  contactPerson: string;
  lastContact: string;
}

@Component({
  selector: 'app-shield-deal-details-card',
  templateUrl: './deal-details-card.component.html',
  styleUrls: ['./deal-details-card.component.scss']
})
export class DealDetailsCardComponent implements OnInit, OnDestroy {
  private filterSubscription: Subscription = new Subscription();
  
  // Mock deal details data
  originalDealDetails: DealDetail[] = [
    {
      id: 1,
      companyName: 'Tech Solutions Inc',
      stage: 'Negotiation',
      description: 'Enterprise software solution for HR management',
      amount: 125000,
      stageDays: 15,
      contactPerson: 'John Smith',
      lastContact: '2023-06-10'
    },
    {
      id: 2,
      companyName: 'Global Enterprises',
      stage: 'Decision Making',
      description: 'Cloud infrastructure services migration',
      amount: 85000,
      stageDays: 8,
      contactPerson: 'Sarah Johnson',
      lastContact: '2023-06-12'
    },
    {
      id: 3,
      companyName: 'Innovative Systems',
      stage: 'Value Proposition',
      description: 'Custom AI implementation for logistics',
      amount: 210000,
      stageDays: 22,
      contactPerson: 'Mike Brown',
      lastContact: '2023-06-05'
    }
  ];
  
  dealDetails: DealDetail[] = [...this.originalDealDetails];

  // Stage colors
  stageColors: { [key: string]: string } = {
    'Prospecting': '#93c5fd',
    'Qualification': '#60a5fa',
    'Needs Analysis': '#3b82f6',
    'Value Proposition': '#1d4ed8',
    'Decision Making': '#0a192f',
    'Negotiation': '#ff6b35'
  };

  constructor(private filterService: DashboardFilterService) { }

  ngOnInit(): void {
    // Subscribe to filter changes
    this.filterSubscription.add(
      this.filterService.filterState$.subscribe(filters => {
        this.updateDealData(filters);
      })
    );
  }
  
  ngOnDestroy(): void {
    // Unsubscribe from all subscriptions
    this.filterSubscription.unsubscribe();
  }

  // Update deal data based on filters
  updateDealData(filters: any): void {
    // Simulate data change based on filters
    // In a real implementation, this would fetch new data from an API
    
    if (filters.salesRep || filters.partner || filters.tractionChannel || filters.subProductLine) {
      // Apply filter effects (simplified logic)
      const filteredData = this.originalDealDetails.filter(item => {
        // Simple filtering logic - in a real app, this would be more sophisticated
        return Math.random() > 0.2; // Randomly filter out some items
      });
      
      this.dealDetails = filteredData;
    } else {
      // No filters applied, show all data
      this.dealDetails = [...this.originalDealDetails];
    }
  }

  // Get stage color
  getStageColor(stage: string): string {
    return this.stageColors[stage] || '#64748b';
  }

  // Format currency
  formatCurrency(value: number): string {
    return new Intl.NumberFormat('en-US', {
      style: 'currency',
      currency: 'USD',
      minimumFractionDigits: 0,
      maximumFractionDigits: 0
    }).format(value);
  }
}