import { V266_LEASE_DOCUMENT_HTML } from './v266-lease-document.js';
import { TRIAL_AGREEMENT_DOCUMENT_HTML } from './trial-agreement-document.js';
import { TRIAL_AGREEMENT_18T_DOCUMENT_HTML } from './trial-agreement-18t-document.js';
import { emptySectionMap, splitMonolithicHtml } from './contract-template-section-html.js';

function getLeaseDocumentSectionMap() {
	if (typeof V266_LEASE_DOCUMENT_HTML !== 'undefined' && V266_LEASE_DOCUMENT_HTML) {
		return splitMonolithicHtml(V266_LEASE_DOCUMENT_HTML);
	}
	return emptySectionMap();
}

function isTrialAgreementFileName(name) {
	return /商用车试用协议/i.test(String(name || ''));
}

function isTrialAgreement18tFileName(name) {
	return /商用车试用协议.*现代18吨|现代18吨.*商用车试用协议/i.test(String(name || ''));
}

function getTrialAgreementHtml(fileName) {
	if (isTrialAgreement18tFileName(fileName)) {
		return typeof TRIAL_AGREEMENT_18T_DOCUMENT_HTML !== 'undefined' ? TRIAL_AGREEMENT_18T_DOCUMENT_HTML : '';
	}
	if (isTrialAgreementFileName(fileName)) {
		return typeof TRIAL_AGREEMENT_DOCUMENT_HTML !== 'undefined' ? TRIAL_AGREEMENT_DOCUMENT_HTML : '';
	}
	return '';
}

function getTrialAgreementSectionMap(fileName) {
	var html = getTrialAgreementHtml(fileName);
	if (!html) return emptySectionMap();
	return splitMonolithicHtml(html);
}

function createWordDocument(params) {
	return Object.assign({
		category: 'lease',
		contractType: '',
		fileName: '未命名合同.docx',
		versionName: '未命名合同',
		versionNo: null,
		status: 'draft',
		enabled: false,
		versionLogs: [],
		sectionContents: emptySectionMap(),
		sectionVehicleBindings: {},
		remark: '',
	}, params || {});
}

/** 冷启动种子：未打开模板管理页时租赁创建等场景仍可列出已启用模板 */
export function buildInitialWordTemplates() {
	var v266 = getLeaseDocumentSectionMap();
	var trialBase = getTrialAgreementSectionMap('商用车试用协议.docx');
	var trial18t = getTrialAgreementSectionMap('商用车试用协议-现代18吨.docx');
	var model18t = [{ brand: '现代', model: '18吨氢燃料电池车' }];
	var model45t = [{ brand: '现代', model: '帕力安牌4.5吨冷链车' }];
	var model96 = [{ brand: '苏龙', model: '9.6米氢燃料电池车' }];
	var fullSections = Object.assign(emptySectionMap(), v266);
	return [
		createWordDocument({
			id: 'doc-1',
			contractType: '现代18吨正式合同',
			fileName: '现代18吨车型-数智中心版.docx',
			versionName: '现代18吨车型-数智中心版',
			versionNo: 'v1.00',
			status: 'published',
			enabled: true,
			versionLogs: [{
				id: 'log-doc-1-v0',
				versionNo: 'v0.99',
				action: '发布变更',
				operator: '法务-王静',
				time: '2026-05-20 16:40',
				remark: '数智中心版条款调整前的归档快照',
				snapshot: {
					fileName: '现代18吨车型-数智中心版.docx',
					versionName: '现代18吨车型-数智中心版',
					versionNo: 'v0.99',
					sectionContents: Object.assign(emptySectionMap(), v266),
					sectionVehicleBindings: { lease_order: model18t },
					remark: '历史快照（原型）',
					archivedAt: '2026-05-20 16:40',
				},
			}],
			sectionContents: fullSections,
			sectionVehicleBindings: {
				lease_order: model18t,
				dynamic_supervision: model96,
				vehicle_spec: model45t,
			},
			creator: '法务-王静',
			createTime: '2026-05-25 09:13',
			updater: '法务-王静',
			updateTime: '2026-05-25 09:13',
			remark: 'V26.6 现代18吨数智中心版标准合同',
		}),
		createWordDocument({
			id: 'doc-2',
			contractType: '正式合同',
			fileName: '2026年标准商用车租赁合同.docx',
			versionName: '2026年标准商用车租赁合同',
			versionNo: 'v1.01',
			status: 'published',
			enabled: true,
			sectionContents: fullSections,
			sectionVehicleBindings: {},
			creator: '法务-王静',
			createTime: '2026-03-15 14:30',
			updater: '法务-王静',
			updateTime: '2026-03-15 14:30',
			remark: '通用主体条款',
		}),
		createWordDocument({
			id: 'doc-3',
			contractType: '正式合同',
			fileName: '2026年二季度草案.docx',
			versionName: '2026年二季度草案',
			versionNo: null,
			status: 'draft',
			sectionContents: fullSections,
			sectionVehicleBindings: {},
			creator: '法务-李明',
			createTime: '2026-06-01 09:15',
			updater: '法务-李明',
			updateTime: '2026-06-01 09:15',
			remark: '待法务审核发布',
		}),
		createWordDocument({
			id: 'doc-4',
			contractType: '试用合同',
			fileName: '商用车试用协议.docx',
			versionName: '商用车试用协议',
			versionNo: 'v1.00',
			status: 'published',
			enabled: true,
			sectionContents: Object.assign(emptySectionMap(), trialBase),
			sectionVehicleBindings: {},
			creator: '法务-王静',
			createTime: '2026-06-20 10:00',
			updater: '法务-王静',
			updateTime: '2026-06-20 10:00',
			remark: '通用商用车试用协议范本',
		}),
		createWordDocument({
			id: 'doc-5',
			contractType: '现代18吨试用合同',
			fileName: '商用车试用协议-现代18吨.docx',
			versionName: '商用车试用协议-现代18吨',
			versionNo: 'v1.00',
			status: 'published',
			enabled: false,
			sectionContents: Object.assign(emptySectionMap(), trial18t),
			sectionVehicleBindings: {},
			creator: '法务-李明',
			createTime: '2026-06-25 14:20',
			updater: '法务-李明',
			updateTime: '2026-06-25 14:20',
			remark: '现代18吨车型试用协议；含完整安全附件',
		}),
	];
}
