export type CrmStatusTone = 'default' | 'secondary' | 'outline' | 'destructive';

export interface CrmBranch {
  id: string;
  code: string;
  name: string;
  region: string;
}

export interface CrmSalesperson {
  id: string;
  name: string;
  nickname: string;
  branchId: string;
  role: 'sales' | 'sale_admin' | 'manager';
}

export interface CustomerContact {
  id: string;
  customerId: string;
  name: string;
  position: string;
  email: string;
  phone: string;
  lineId?: string;
  isPrimary?: boolean;
}

export interface ContactShareLog {
  id: string;
  contactId: string;
  action: 'SHARE' | 'REVOKE';
  targetUserName: string;
  actorName: string;
  createdAt: string;
}

export interface ContactShare {
  id: string;
  contactId: string;
  sharedWithUserId: string;
  sharedWithUserName: string;
  permission: 'view' | 'edit';
  createdAt: string;
}

export interface Customer {
  id: string;
  code: string;
  name: string;
  taxId: string;
  address: string;
  province: string;
  district: string;
  subDistrict: string;
  postalCode: string;
  phone: string;
  fax?: string;
  email: string;
  customerType: 'developer' | 'contractor' | 'owner' | 'consultant';
  customerStatus: 'active' | 'prospect' | 'inactive';
  branchId: string;
  contactIds: string[];
}

export type EnquiryStatus =
  | 'new'
  | 'qualifying'
  | 'requirement'
  | 'follow_up'
  | 'converted'
  | 'closed_lost'
  | 'cancelled';

export interface EnquiryActivity {
  id: string;
  type: 'CALL' | 'MEETING' | 'VISIT' | 'NOTE' | 'UPDATE' | 'CONVERT';
  title: string;
  detail: string;
  actorName: string;
  createdAt: string;
}

export interface Enquiry {
  id: string;
  code: string;
  title: string;
  customerId: string;
  contactId: string;
  branchId: string;
  salesmanId: string;
  productType: 'crane' | 'dockdoor' | 'solarcell';
  status: EnquiryStatus;
  requirementSummary: string;
  projectLocation: string;
  chancePercent: number;
  competitor: string;
  expectedValue: number;
  dueDate: string;
  createdAt: string;
  updatedAt: string;
  quotationIds: string[];
  activities: EnquiryActivity[];
}

export type QuotationStatus =
  | 'draft'
  | 'pending_approval'
  | 'approved'
  | 'rejected'
  | 'sent'
  | 'accepted'
  | 'lost'
  | 'cancelled'
  | 'revised';

export interface QuotationCustomerRole {
  role: 'owner' | 'consultant' | 'contractor' | 'billing';
  customerId: string;
  contactId?: string;
}

export interface QuotationItem {
  id: string;
  topic: string;
  description: string;
  quantity: number;
  unit: string;
  unitPrice: number;
  amount: number;
}

export interface QuotationTopic {
  id: string;
  type: 'scope' | 'exclusion' | 'payment';
  label: string;
  items: string[];
}

export interface QuotationFollowUp {
  id: string;
  title: string;
  dueDate: string;
  ownerName: string;
  status: 'open' | 'done';
}

export interface QuotationAttachment {
  id: string;
  fileName: string;
  fileType: 'pdf' | 'doc' | 'xls' | 'zip';
  uploadedAt: string;
}

export interface ApprovalStep {
  id: string;
  level: number;
  approverName: string;
  approverPosition: string;
  status: 'pending' | 'approved' | 'rejected';
  actedAt?: string;
  comment?: string;
}

export interface Quotation {
  id: string;
  code: string;
  enquiryId: string;
  quotationDate: string;
  validUntil: string;
  quotationType: 'budgetary' | 'official' | 'service';
  project: string;
  siteLocation: string;
  customerRoles: QuotationCustomerRole[];
  salesmanId: string;
  saleAdminId: string;
  branchId: string;
  status: QuotationStatus;
  revision: number;
  totalAmount: number;
  chancePercent: number;
  isHotProject: boolean;
  approvedPdfUrl?: string;
  approvalSnapshot?: string;
  items: QuotationItem[];
  topics: QuotationTopic[];
  followUps: QuotationFollowUp[];
  attachments: QuotationAttachment[];
  approvalSteps: ApprovalStep[];
  auditEventIds: string[];
}

export interface ApprovalItem {
  id: string;
  quotationId: string;
  quotationCode: string;
  amount: number;
  productType: string;
  branchId: string;
  submitterName: string;
  approverLevel: number;
  status: 'pending' | 'approved' | 'rejected';
}

export interface AuditEvent {
  id: string;
  entityType: 'customer' | 'enquiry' | 'quotation' | 'approval';
  entityId: string;
  action: 'CREATE' | 'UPDATE' | 'SEND' | 'APPROVE' | 'REJECT' | 'CONVERT' | 'REVISE';
  actorName: string;
  detail: string;
  createdAt: string;
}

export interface DocumentSequence {
  id: string;
  documentType: 'enquiry' | 'quotation' | 'quotation_revision';
  prefix: string;
  period: string;
  branchId: string;
  currentNumber: number;
  paddingLength: number;
}

export interface MasterOption {
  id: string;
  group: 'status' | 'product_type' | 'payment_term' | 'currency' | 'tax_rate';
  code: string;
  label: string;
  description?: string;
  active: boolean;
}

export interface TemplateMapping {
  id: string;
  key: string;
  placeholder: string;
  sourceField: string;
}

export interface QuotationTemplate {
  id: string;
  name: string;
  version: string;
  branchId?: string;
  description: string;
  mappings: TemplateMapping[];
}

export interface CrmDashboardMetric {
  id: string;
  label: string;
  value: string;
  change: string;
  trend: 'up' | 'down' | 'flat';
  tone?: CrmStatusTone;
}

export interface CrmChartDatum {
  label: string;
  value: number;
  secondaryValue?: number;
}

export interface CrmDashboardData {
  metrics: CrmDashboardMetric[];
  enquiryPipeline: CrmChartDatum[];
  quotationPipeline: CrmChartDatum[];
  salesPerformance: CrmChartDatum[];
  competitorAnalysis: CrmChartDatum[];
  wonLostTrend: CrmChartDatum[];
  pendingApprovals: ApprovalItem[];
  hotQuotations: Quotation[];
  dueFollowUps: QuotationFollowUp[];
}

export interface CustomerFilters {
  page?: number;
  limit?: number;
  search?: string;
  status?: string;
  branch?: string;
  sort?: string;
}

export interface EnquiryFilters {
  page?: number;
  limit?: number;
  search?: string;
  status?: string;
  productType?: string;
  salesman?: string;
  dateFrom?: string;
  dateTo?: string;
  sort?: string;
}

export interface QuotationFilters {
  page?: number;
  limit?: number;
  search?: string;
  status?: string;
  quotationType?: string;
  salesman?: string;
  hot?: string;
  dateFrom?: string;
  dateTo?: string;
  sort?: string;
}

export interface ApprovalFilters {
  page?: number;
  limit?: number;
  branch?: string;
  status?: string;
}

export interface PaginatedResponse<T> {
  items: T[];
  totalItems: number;
  summary?: {
    activeFilters: number;
  };
}

export interface CustomerDetailResponse {
  customer: Customer;
  contacts: CustomerContact[];
  shares: ContactShare[];
  shareLogs: ContactShareLog[];
  enquiries: Enquiry[];
  quotations: Quotation[];
  auditEvents: AuditEvent[];
  branch: CrmBranch;
}

export interface EnquiryDetailResponse {
  enquiry: Enquiry;
  customer: Customer;
  contact: CustomerContact;
  quotations: Quotation[];
  branch: CrmBranch;
  salesman: CrmSalesperson;
}

export interface QuotationDetailResponse {
  quotation: Quotation;
  enquiry: Enquiry;
  customers: Customer[];
  contacts: CustomerContact[];
  branch: CrmBranch;
  salesman: CrmSalesperson;
  saleAdmin: CrmSalesperson;
  auditEvents: AuditEvent[];
}

export interface CrmReferenceData {
  branches: CrmBranch[];
  salespersons: CrmSalesperson[];
  masterOptions: MasterOption[];
  sequences: DocumentSequence[];
  templates: QuotationTemplate[];
}

export interface CustomerMutationPayload {
  id?: string;
  code: string;
  name: string;
  taxId: string;
  email: string;
  phone: string;
  customerType: Customer['customerType'];
  customerStatus: Customer['customerStatus'];
  branchId: string;
  address: string;
  province: string;
  district: string;
  subDistrict: string;
  postalCode: string;
}

export interface ApprovalActionPayload {
  quotationId: string;
  comment?: string;
}

export interface ContactSharePayload {
  contactId: string;
  sharedWithUserId: string;
  sharedWithUserName: string;
  permission: 'view' | 'edit';
}
