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

export interface OpportunityProjectPartyRecord {
  id: string;
  organizationId: string;
  opportunityId: string;
  customerId: string;
  role: string;
  roleCode: string;
  roleLabel: string | null;
  remark: string | null;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

export interface OpportunityProjectPartyListItem extends OpportunityProjectPartyRecord {
  customerName: string;
  customerCode: string;
}

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

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

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

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

export interface OpportunityAttachmentRecord {
  id: string;
  organizationId: string;
  opportunityId: string;
  category: string;
  fileName: string;
  originalFileName: string;
  storageProvider: string;
  storageKey: string;
  fileSize: number | null;
  fileType: string | null;
  description: string | null;
  uploadedAt: string;
  uploadedBy: string;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

export type OpportunityPipelineStage = 'lead' | 'opportunity' | 'closed_won' | 'closed_lost';
export type OpportunityOutcomeStatus = 'open' | 'won' | 'lost' | 'cancelled' | 'no_quotation';

export interface OpportunityRecord {
  id: string;
  organizationId: string;
  branchId: string | null;
  leadId: string | null;
  code: string;
  customerId: string;
  contactId: string | null;
  title: string;
  description: string | null;
  requirement: string | null;
  projectName: string | null;
  projectLocation: string | null;
  productType: string;
  status: string;
  stage: string;
  outcomeStatus: OpportunityOutcomeStatus;
  priority: string;
  leadChannel: string | null;
  estimatedValue: number | null;
  chancePercent: number | null;
  expectedCloseDate: string | null;
  projectCloseDate: string | null;
  deliveryDate: string | null;
  competitor: string | null;
  source: string | null;
  notes: string | null;
  isHotProject: boolean;
  hotProjectAutoSuggested: boolean;
  hotProjectManuallyOverridden: boolean;
  isActive: boolean;
  pipelineStage: OpportunityPipelineStage;
  closedAt: string | null;
  closedWonAt: string | null;
  closedLostAt: string | null;
  closedByUserId: string | null;
  closedByName: string | null;
  poNumber: string | null;
  poDate: string | null;
  poAmount: number | null;
  poCurrency: string | null;
  lostReason: string | null;
  lostReasonCode: string | null;
  lostReasonLabel: string | null;
  lostDetail: string | null;
  lostCompetitor: string | null;
  lostRemark: string | null;
  cancelReason: string | null;
  noQuotationReason: string | null;
  assignedToUserId: string | null;
  assignedToName: string | null;
  assignedAt: string | null;
  assignedBy: string | null;
  assignedByName: string | null;
  assignmentRemark: string | null;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  createdBy: string;
  updatedBy: string;
}

export interface OpportunityListItem extends OpportunityRecord {
  customerName: string;
  contactName: string | null;
  sourceLeadCode?: string | null;
  sourceLeadProjectName?: string | null;
  lastFollowupDate?: string | null;
  followupCount: number;
  quotationCount: number;
  nextFollowupDate: string | null;
  createdByName: string | null;
}

export interface OpportunityFollowupRecord {
  id: string;
  organizationId: string;
  opportunityId: string;
  followupDate: string;
  followupType: string;
  contactId: string | null;
  outcome: string | null;
  notes: string | null;
  nextFollowupDate: string | null;
  nextAction: string | null;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  createdBy: string;
  updatedBy: string;
}

export type OpportunityActivityRecord = CrmAuditLogRecord;

export interface OpportunityReferenceData {
  branches: OpportunityBranchOption[];
  statuses: OpportunityOption[];
  stages: OpportunityOption[];
  outcomeStatuses: OpportunityOption[];
  productTypes: OpportunityOption[];
  priorities: OpportunityOption[];
  leadChannels: OpportunityOption[];
  followupTypes: OpportunityOption[];
  lostReasons: OpportunityOption[];
  cancelReasons: OpportunityOption[];
  noQuotationReasons: OpportunityOption[];
  projectPartyRoles: OpportunityOption[];
  customers: OpportunityCustomerLookup[];
  contacts: OpportunityContactLookup[];
  assignableUsers: OpportunityAssignableUserLookup[];
}

export interface OpportunityFilters {
  page?: number;
  limit?: number;
  search?: string;
  pipelineStage?: OpportunityPipelineStage;
  status?: string;
  stage?: string;
  outcomeStatus?: string;
  productType?: string;
  priority?: string;
  branch?: string;
  customer?: string;
  lostReason?: string;
  closedDateFrom?: string;
  closedDateTo?: string;
  sort?: string;
}

export interface OpportunityListResponse {
  success: boolean;
  time: string;
  message: string;
  totalItems: number;
  offset: number;
  limit: number;
  items: OpportunityListItem[];
}

export interface OpportunityDetailResponse {
  success: boolean;
  time: string;
  message: string;
  opportunity: OpportunityRecord;
  activity: OpportunityActivityRecord[];
}

export interface OpportunityProjectPartiesResponse {
  success: boolean;
  time: string;
  message: string;
  items: OpportunityProjectPartyListItem[];
}

export interface OpportunityFollowupsResponse {
  success: boolean;
  time: string;
  message: string;
  items: OpportunityFollowupRecord[];
}

export interface OpportunityAttachmentsResponse {
  success: boolean;
  time: string;
  message: string;
  items: OpportunityAttachmentRecord[];
}

export interface OpportunityProjectPartyMutationPayload {
  customerId: string;
  role: string;
  remark?: string | null;
}

export interface OpportunityMutationPayload {
  customerId: string;
  contactId?: string | null;
  assignedToUserId?: string | null;
  title: string;
  description?: string;
  requirement?: string;
  projectName?: string;
  projectLocation?: string;
  branchId?: string | null;
  productType: string;
  status: string;
  priority: string;
  leadChannel?: string | null;
  estimatedValue?: number | null;
  chancePercent?: number | null;
  expectedCloseDate?: string | null;
  projectCloseDate?: string | null;
  deliveryDate?: string | null;
  competitor?: string;
  source?: string;
  isHotProject?: boolean;
  hotProjectAutoSuggested?: boolean;
  hotProjectManuallyOverridden?: boolean;
  notes?: string;
  isActive?: boolean;
  projectParties?: OpportunityProjectPartyMutationPayload[];
}

export interface OpportunityFollowupMutationPayload {
  followupDate: string;
  followupType: string;
  contactId?: string | null;
  outcome?: string;
  notes?: string;
  nextFollowupDate?: string | null;
  nextAction?: string;
}

export interface OpportunityAssignmentMutationPayload {
  assignedToUserId: string;
  remark?: string;
}

export interface OpportunityMarkWonPayload {
  poNumber: string;
  poDate: string;
  poAmount?: number | null;
  poCurrency?: string | null;
  remark?: string | null;
}

export interface OpportunityMarkLostPayload {
  lostReason: string;
  lostDetail?: string | null;
  lostCompetitor?: string | null;
  lostRemark?: string | null;
}

export interface OpportunityMarkCancelledPayload {
  cancelReason: string;
}

export interface OpportunityMarkNoQuotationPayload {
  noQuotationReason: string;
}

export interface OpportunityReopenPayload {
  reopenReason: string;
}

export interface OpportunityCustomerRelationItem {
  id: string;
  code: string;
  title: string;
  status: string;
  productType: string;
  updatedAt: string;
}

export interface MutationSuccessResponse {
  success: boolean;
  message: string;
}
import type { CrmAuditLogRecord } from '@/features/crm/audit-log/types';
