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

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

export interface QuotationCustomerLookup {
  id: string;
  code: string;
  name: string;
  branchId: string | null;
}

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

export interface QuotationEnquiryLookup {
  id: string;
  code: string;
  customerId: string;
  contactId: string | null;
  title: string;
  projectName: string | null;
  projectLocation: string | null;
  branchId: string | null;
}

export interface QuotationSalesmanLookup {
  id: string;
  name: string;
}

export interface QuotationApprovedArtifactSummary {
  id: string;
  fileName: string;
  contentType: string;
  fileSize: number | null;
  checksum: string | null;
  status: string;
  generatedAt: string;
  generatedBy: string;
  generatedByName: string | null;
  lockedAt: string | null;
  lockedBy: string | null;
  lockedByName: string | null;
}

export interface QuotationRecord {
  id: string;
  organizationId: string;
  branchId: string | null;
  code: string;
  enquiryId: string | null;
  customerId: string;
  contactId: string | null;
  quotationDate: string;
  validUntil: string | null;
  quotationType: string;
  projectName: string | null;
  projectLocation: string | null;
  attention: string | null;
  reference: string | null;
  notes: string | null;
  status: string;
  revision: number;
  parentQuotationId: string | null;
  revisionRemark: string | null;
  currency: string;
  exchangeRate: number;
  subtotal: number;
  discount: number;
  discountType: string | null;
  taxRate: number;
  taxAmount: number;
  totalAmount: number;
  chancePercent: number | null;
  isHotProject: boolean;
  competitor: string | null;
  salesmanId: string | null;
  isSent: boolean;
  sentAt: string | null;
  sentVia: string | null;
  approvedAt: string | null;
  approvedArtifactId: string | null;
  approvedPdfUrl: string | null;
  approvedSnapshot: unknown | null;
  approvedTemplateVersionId: string | null;
  approvedArtifact: QuotationApprovedArtifactSummary | null;
  hasLegacyApprovedPdf: boolean;
  acceptedAt: string | null;
  rejectedAt: string | null;
  rejectionReason: string | null;
  isActive: boolean;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  createdBy: string;
  updatedBy: string;
}

export interface QuotationListItem extends QuotationRecord {
  customerName: string;
  contactName: string | null;
  enquiryCode: string | null;
  itemCount: number;
  revisionLabel: string;
}

export interface QuotationItemRecord {
  id: string;
  organizationId: string;
  quotationId: string;
  itemNumber: number;
  productType: string;
  description: string;
  quantity: number;
  unit: string | null;
  unitPrice: number;
  discount: number;
  discountType: string | null;
  taxRate: number;
  totalPrice: number;
  notes: string | null;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

export interface QuotationCustomerRecord {
  id: string;
  organizationId: string;
  quotationId: string;
  customerId: string;
  role: string;
  roleCode: string;
  roleLabel: string | null;
  isPrimary: boolean;
  remark: string | null;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

export interface QuotationCustomerListItem extends QuotationCustomerRecord {
  customerName: string;
  customerCode: string;
}

export interface QuotationTopicItemRecord {
  id: string;
  organizationId: string;
  topicId: string;
  content: string;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

export interface QuotationTopicRecord {
  id: string;
  organizationId: string;
  quotationId: string;
  topicType: string;
  title: string;
  sortOrder: number;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
  items: QuotationTopicItemRecord[];
}

export interface QuotationFollowupRecord {
  id: string;
  organizationId: string;
  quotationId: 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 interface QuotationAttachmentRecord {
  id: string;
  organizationId: string;
  quotationId: string;
  fileName: string;
  originalFileName: string;
  filePath: string;
  fileSize: number | null;
  fileType: string | null;
  description: string | null;
  uploadedAt: string;
  uploadedBy: string;
  createdAt: string;
  updatedAt: string;
  deletedAt: string | null;
}

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

export interface QuotationRelationItem {
  id: string;
  code: string;
  status: string;
  quotationType: string;
  totalAmount: number;
  updatedAt: string;
}

export interface QuotationReferenceData {
  branches: QuotationBranchOption[];
  statuses: QuotationOption[];
  quotationTypes: QuotationOption[];
  currencies: QuotationOption[];
  discountTypes: QuotationOption[];
  units: QuotationOption[];
  sentVias: QuotationOption[];
  projectPartyRoles: QuotationOption[];
  topicTypes: QuotationOption[];
  followupTypes: QuotationOption[];
  productTypes: QuotationOption[];
  customers: QuotationCustomerLookup[];
  contacts: QuotationContactLookup[];
  enquiries: QuotationEnquiryLookup[];
  salesmen: QuotationSalesmanLookup[];
}

export interface QuotationFilters {
  page?: number;
  limit?: number;
  search?: string;
  status?: string;
  quotationType?: string;
  branch?: string;
  customer?: string;
  enquiry?: string;
  hot?: string;
  sort?: string;
}

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

export interface QuotationDetailResponse {
  success: boolean;
  time: string;
  message: string;
  quotation: QuotationRecord;
  activity: QuotationActivityRecord[];
}

export interface QuotationItemsResponse {
  success: boolean;
  time: string;
  message: string;
  items: QuotationItemRecord[];
}

export interface QuotationCustomersResponse {
  success: boolean;
  time: string;
  message: string;
  items: QuotationCustomerListItem[];
}

export interface QuotationTopicsResponse {
  success: boolean;
  time: string;
  message: string;
  items: QuotationTopicRecord[];
}

export interface QuotationFollowupsResponse {
  success: boolean;
  time: string;
  message: string;
  items: QuotationFollowupRecord[];
}

export interface QuotationAttachmentsResponse {
  success: boolean;
  time: string;
  message: string;
  items: QuotationAttachmentRecord[];
}

export interface QuotationRevisionsResponse {
  success: boolean;
  time: string;
  message: string;
  items: QuotationRelationItem[];
}

export interface QuotationMutationPayload {
  enquiryId?: string | null;
  customerId: string;
  contactId?: string | null;
  quotationDate: string;
  validUntil?: string | null;
  quotationType: string;
  projectName?: string;
  projectLocation?: string;
  attention?: string;
  branchId?: string | null;
  currency: string;
  exchangeRate?: number | null;
  status: string;
  chancePercent?: number | null;
  isHotProject?: boolean;
  competitor?: string;
  reference?: string;
  notes?: string;
  salesmanId?: string | null;
  discount?: number | null;
  discountType?: string | null;
  taxRate?: number | null;
  isActive?: boolean;
  sentVia?: string | null;
  revisionRemark?: string | null;
  projectParties?: QuotationCustomerMutationPayload[];
}

export interface QuotationItemMutationPayload {
  productType: string;
  description: string;
  quantity: number;
  unit?: string;
  unitPrice: number;
  discount?: number | null;
  discountType?: string | null;
  taxRate?: number | null;
  notes?: string;
  sortOrder?: number | null;
}

export interface QuotationCustomerMutationPayload {
  customerId: string;
  role: string;
  isPrimary?: boolean;
  remark?: string | null;
}

export interface QuotationTopicMutationPayload {
  id?: string;
  topicType: string;
  title: string;
  sortOrder?: number | null;
  items: Array<{
    id?: string;
    content: string;
    sortOrder?: number | null;
  }>;
}

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

export interface QuotationAttachmentMutationPayload {
  id?: string;
  fileName: string;
  originalFileName: string;
  filePath: string;
  fileSize?: number | null;
  fileType?: string | null;
  description?: string;
}

export interface QuotationRevisionMutationPayload {
  revisionRemark?: string;
}

export interface MutationSuccessResponse {
  success: boolean;
  message: string;
}
