export interface LeadOption {
  id: string;
  code: string;
  label: string;
  value: string | null;
}

export interface LeadBranchOption {
  id: string;
  code: string;
  name: string;
  value: string | null;
}

export interface LeadAssignableUserLookup {
  id: string;
  name: string;
  membershipRole: 'admin' | 'user';
  businessRole: string;
}

export interface LeadCustomerLookup {
  id: string;
  code: string;
  name: string;
  branchId: string | null;
  ownerUserId: string | null;
  ownerName: string | null;
}

export interface LeadContactLookup {
  id: string;
  customerId: string;
  name: string;
  email: string | null;
  mobile: string | null;
  isPrimary: boolean;
}

export interface LeadLinkedOpportunitySummary {
  id: string;
  code: string;
  title: string;
  status: string;
  assignedToUserId: string | null;
  assignedToName: string | null;
  updatedAt: string;
}

export interface LeadActivityRecord {
  id: string;
  action: string;
  entityType: string;
  entityId: string;
  createdAt: string;
  userId: string;
  actorName: string | null;
}

export interface LeadReferenceData {
  awarenesses: LeadOption[];
  statuses: LeadOption[];
  followupStatuses: LeadOption[];
  lostReasons: LeadOption[];
  productTypes: LeadOption[];
  priorities: LeadOption[];
  branches: LeadBranchOption[];
  customers: LeadCustomerLookup[];
  contacts: LeadContactLookup[];
  assignableUsers: LeadAssignableUserLookup[];
}

export interface CreateLeadInput {
  branchId?: string | null;
  customerId?: string | null;
  contactId?: string | null;
  description?: string | null;
  projectName?: string | null;
  projectLocation?: string | null;
  productType?: string | null;
  priority?: string | null;
  estimatedValue?: number | null;
  awarenessId?: string | null;
  status: string;
  followupStatus?: string | null;
  lostReason?: string | null;
  outcome?: 'open' | 'won' | 'lost';
  ownerMarketingUserId?: string | null;
}

export interface UpdateLeadInput {
  branchId?: string | null;
  customerId?: string | null;
  contactId?: string | null;
  description?: string | null;
  projectName?: string | null;
  projectLocation?: string | null;
  productType?: string | null;
  priority?: string | null;
  estimatedValue?: number | null;
  awarenessId?: string | null;
  status?: string;
  followupStatus?: string | null;
  lostReason?: string | null;
  outcome?: 'open' | 'won' | 'lost';
  ownerMarketingUserId?: string | null;
}

export interface LeadListFilters {
  page?: number;
  limit?: number;
  search?: string;
  branchId?: string;
  customerId?: string;
  status?: string;
  awarenessId?: string;
  followupStatus?: string;
  outcome?: string;
  ownerMarketingUserId?: string;
  sort?: string;
}

export interface CreateLeadFollowupInput {
  followupDate: string;
  followupStatus: string;
  note?: string | null;
  nextFollowupDate?: string | null;
}

export interface AssignLeadInput {
  leadId: string;
  salesOwnerId: string;
  assignmentRemark?: string | null;
}

export interface LeadSummary {
  id: string;
  organizationId: string;
  branchId: string | null;
  code: string;
  customerId: string | null;
  customerName: string | null;
  contactId: string | null;
  contactName: string | null;
  description: string | null;
  projectName: string | null;
  projectLocation: string | null;
  productType: string | null;
  priority: string | null;
  estimatedValue: number | null;
  awarenessId: string | null;
  awarenessLabel: string | null;
  status: string;
  statusLabel: string | null;
  followupStatus: string | null;
  followupStatusLabel: string | null;
  lostReason: string | null;
  lostReasonLabel: string | null;
  outcome: string;
  ownerMarketingUserId: string | null;
  ownerMarketingName: string | null;
  assignedSalesOwnerId: string | null;
  assignedSalesOwnerName: string | null;
  suggestedSalesOwnerId: string | null;
  suggestedSalesOwnerName: string | null;
  assignedAt: string | null;
  assignedBy: string | null;
  assignedByName: string | null;
  assignmentRemark: string | null;
  createdBy: string;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

export interface LeadDetail extends LeadSummary {
  relatedOpportunityCount: number;
  linkedOpportunities: LeadLinkedOpportunitySummary[];
  activity: LeadActivityRecord[];
}

export interface LeadFollowupSummary {
  id: string;
  leadId: string;
  followupDate: string;
  followupStatus: string;
  followupStatusLabel: string | null;
  note: string | null;
  nextFollowupDate: string | null;
  createdAt: string;
  createdBy: string;
  createdByName: string | null;
}

export interface AssignLeadResult {
  leadId: string;
  opportunityId: string;
  opportunityCode: string;
  reusedExistingOpportunity: boolean;
}

export interface LeadListResponse {
  success: boolean;
  time: string;
  message: string;
  totalItems: number;
  offset: number;
  limit: number;
  items: LeadSummary[];
  referenceData: LeadReferenceData;
}

export interface LeadDetailResponse {
  success: boolean;
  time: string;
  message: string;
  lead: LeadDetail;
  followups: LeadFollowupSummary[];
  referenceData: LeadReferenceData;
}

export interface LeadMutationSuccessResponse {
  success: boolean;
  message: string;
  lead?: LeadDetail;
  followup?: LeadFollowupSummary;
}

