import { Contact } from "@/redux/api/contactApi";
import { SuggestedProject, SuggestedProperty } from "@/redux/api/leadApi";

export type Location = {
  id: string;
  name: string;
};

export type LeadActivityCounts = {
  callAttempts: number;
  meetings: number;
  siteVisits: number;
};

export type Lead = {
  id: string;
  leadScore: number;
  priority: string;
  interestType: string;
  name: string;
  email: string;
  notes?: string;
  reason?: string;
  leadStage: { color?: string; id?: string };
  project: { projectName?: string; id?: string };
  phoneNumber: string;
  lastContacted: string | Date;
  // assignedTo: string;
  contact?: Contact;
  createdOn: string | Date;
  createdAt: string | Date;
  updatedAt: string | Date;
  leadStageName: string;
  company?: { id?: string; name?: string };
  source?: { id?: string; name?: string };
  updatedOn?: string | Date;
  contact?: {
    id: string;
    firstName: string;
    lastName: string;
    name: string;
    email: string;
    leadStage: { color?: string; id?: string };
    project: { projectName?: string; id?: string };
    phoneNumber: string;
    lastContacted: string | Date;
    // assignedTo: string;
    contact?: Contact;
    createdOn: string | Date;
    updatedOn: string | Date;
    createdAt: string | Date;
    updatedAt: string | Date;
    leadStageName: string;
    company?: { id?: string; name?: string };
    source?: { id?: string; name?: string };
    contact?: {
      id: string;
      firstName: string;
      lastName: string;
      name: string;
      companyName: string;
      primaryPhone?: {
        number: string;
      };
      phone?: {
        number: string;
        countryCode?: string;
      }[];
      email?: string;
    };
    phone?: {
      number: string;
      countryCode?: string;
    }[];
    email?: string;
  };
  assignedTo?: {
    id: string;
    firstName: string;
    lastName: string;
  };
  unit?: {
    unitNumber?: string;
    id?: string;
  };
  propertyType?: [
    {
      name?: string;
      id?: string;
    },
  ];
  configuration?: [
    {
      name?: string;
      id?: string;
    },
  ];
  budget?: number;
  leaseAmount?: number;
  monthlyRent?: number;
  buyingPreference?: string;
  preferredLocalities?: Location[] | Location;
  preferredCity?: Location;
  preferredState?: Location;
  shortlisted?: SuggestedProperty[];
  removed?: SuggestedProperty[];
  shortlistedProject?: SuggestedProject[];
  removedProject?: SuggestedProject[];
  customFields?: Record<string, any>;
  sourceProjectId?: string;
  sourceProjectName?: string;
  sourceLocality?: string;
  sourceUrl?: string;
  activityCounts?: LeadActivityCounts;
  activityCountsUpdatedAt?: string | Date;
  projectArea?: string;
  projectLocality?: string;
};

export interface LeadActivity {
  id: string;
  name: string;
  description: string;
  points: number;
  defaultPoints: number;
}

export interface MonthlyLeadData {
  count: number;
  year: number;
  month: string;
  monthNum: number;
  changeDirection: string;
  change: number;
}

export interface LeadScoreBucket {
  _id: string; // 'Unknown' or bucket name
  count: number;
}

export interface LeadStage {
  count: number;
  leadStageId: string;
  stageName: string;
}

export interface StageCounts {
  stages: LeadStage[];
  totalCount: number;
}

export interface ConversionRateEntry {
  total: number;
  won: number;
  year: number;
  month: string;
  monthNum: number;
  conversionRate: number;
  changeDirection: string;
  change: number;
}

export interface LeadAnalyticsResponse {
  data: {
    totalPerMonth: MonthlyLeadData[];
    leadScores: LeadScoreBucket[];
    stageCounts: StageCounts[];
    conversionRate: ConversionRateEntry[];
  };
}

/** Payload returned in `data` from POST /lead/bulkUpload (HTTP 200). */
export interface BulkLeadUploadResultData {
  created: number;
  skipped: number;
  totalRequested: number;
  errors: string[];
}

/** Aggregated result after one or more bulk upload batches. */
export interface LeadImportAggregateResult {
  totalCreated: number;
  totalSkipped: number;
  totalRequested: number;
  allErrors: string[];
}
