import type {
  DocumentTemplateMappingWithColumns,
  ResolvedDocumentTemplate
} from '@/features/foundation/document-template/types';
import type { SignatureBlock } from './signature.types';
import type { PdfTopic } from './server/pdf-topic.type';

export interface QuotationDocumentTopicGroup {
  id?: string;
  title: string;
  sortOrder?: number | null;
  items: string[];
}

export interface QuotationDocumentApprovalStep {
  stepNumber: number;
  roleCode: string;
  roleName: string;
  action: string;
  actorName: string | null;
  actedAt: string | null;
  remark: string | null;
}

export interface QuotationDocumentData {
  company: {
    id: string;
    name: string;
    slug: string;
    imageUrl: string | null;
    plan: string;
  };
  branch: {
    id: string;
    code: string;
    label: string;
    value: string | null;
  } | null;
  customer: {
    id: string;
    code: string;
    name: string;
    address: string | null;
    phone: string | null;
    email: string | null;
    taxId: string | null;
  };
  contact: {
    id: string;
    name: string;
    email: string | null;
    mobile: string | null;
    position: string | null;
  } | null;
  quotation: {
    id: string;
    code: string;
    quotationDate: string;
    validUntil: string | null;
    projectName: string | null;
    projectLocation: string | null;
    attention: string | null;
    reference: string | null;
    revision: number;
    revisionLabel: string;
    statusCode: string | null;
    statusLabel: string | null;
    quotationTypeCode: string | null;
    quotationTypeLabel: string | null;
    currency: string | null;
    currencyCode: string | null;
    currencyLabel: string | null;
    exchangeRate: number;
    subtotal: number;
    discount: number;
    discountTypeCode: string | null;
    discountTypeLabel: string | null;
    taxRate: number;
    taxAmount: number;
    totalAmount: number;
    notes: string | null;
  };
  items: Array<{
    id: string;
    itemNumber: number;
    productTypeCode: string | null;
    productTypeLabel: string | null;
    description: string;
    quantity: number;
    unitCode: string | null;
    unitLabel: string | null;
    unitPrice: number;
    discount: number;
    discountTypeCode: string | null;
    taxRate: number;
    totalPrice: number;
    notes: string | null;
  }>;
  quotationCustomers: Array<{
    id: string;
    customerId: string;
    customerName: string;
    customerCode: string;
    roleCode: string;
    roleLabel: string | null;
    isPrimary: boolean;
  }>;
  topics: {
    scope: QuotationDocumentTopicGroup[];
    exclusion: QuotationDocumentTopicGroup[];
    payment: QuotationDocumentTopicGroup[];
    warranty: QuotationDocumentTopicGroup[];
    delivery: QuotationDocumentTopicGroup[];
    other: QuotationDocumentTopicGroup[];
    all: PdfTopic[];
  };
  pdfme: {
    quotation_date: string;
    quotation_price: string;
    exclusion_data: string[][];
    payment_data?: string[][];
    warranty_data?: string[][];
    delivery_data?: string[][];
    topics_data?: string[][];
    topic_inputs?: Record<string, string[][]>;
  };
  approval: {
    requestId: string | null;
    workflowName: string | null;
    status: string | null;
    approvedAt: string | null;
    currentStep: number | null;
    currentStepRoleName: string | null;
    approvers: QuotationDocumentApprovalStep[];
  };
  signatures: SignatureBlock;
  watermarkStatus: string | null;
}

export interface QuotationDocumentDataResponse {
  success: boolean;
  time: string;
  message: string;
  documentData: QuotationDocumentData;
}

export interface QuotationDocumentPreviewResponse {
  success: boolean;
  time: string;
  message: string;
  preview: {
    documentData: QuotationDocumentData;
    template: ResolvedDocumentTemplate;
    mappings: DocumentTemplateMappingWithColumns[];
    templateInput: Record<string, unknown>;
  };
}

export interface ApprovedQuotationSnapshot {
  quotationId: string;
  approvedAt: string | null;
  documentData: QuotationDocumentData;
  templateVersionId: string;
  templateInput: Record<string, unknown>;
  generatedAt: string;
  generatedBy: string;
}
