import type { ScreenData, TimeRange } from './screen';

export const WASTE_COLORS: Record<string, string> = {
	SW62: '#16a34a',
	SW64: '#64748b',
	SW63: '#f59e0b',
	SW61: '#ef4444',
	SW60: '#06b6d4'
};

export const ITEM_COLORS = ['#3b82f6', '#22c55e', '#f59e0b', '#a855f7', '#06b6d4', '#ef4444', '#94a3b8'];

const WASTE_ITEMS = [
	{ code: 'SW62', name: '可回收物' },
	{ code: 'SW64', name: '其他垃圾' },
	{ code: 'SW63', name: '大件垃圾' },
	{ code: 'SW61', name: '有害垃圾' },
	{ code: 'SW60', name: '湿垃圾' }
];

const RECYCLE_NAMES = ['废纸', '塑料', '金属', '织物', '玻璃', '电子产品', '其他可回收'];

/** 与后端 buildTrend 对齐的时间轴 */
export function buildScreenAxis(range: TimeRange): string[] {
	const now = new Date();
	const pad = (n: number) => String(n).padStart(2, '0');
	const fmt = (d: Date) => `${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;

	if (range === 'year') {
		return Array.from({ length: 12 }, (_, i) => `${i + 1}月`);
	}

	let start: Date;
	if (range === 'today') {
		start = new Date(now);
		start.setDate(start.getDate() - 6);
	} else if (range === 'week') {
		start = new Date(now);
		const day = start.getDay() || 7;
		start.setDate(start.getDate() - day + 1);
	} else if (range === 'quarter') {
		start = new Date(now.getFullYear(), Math.floor(now.getMonth() / 3) * 3, 1);
	} else {
		start = new Date(now.getFullYear(), now.getMonth(), 1);
	}

	const axis: string[] = [];
	const cursor = new Date(start);
	while (cursor <= now) {
		axis.push(fmt(cursor));
		cursor.setDate(cursor.getDate() + 1);
	}
	return axis;
}

/** 只保证图表/表格结构完整，数值为 0 */
export function structureScreen(range: TimeRange = 'month'): ScreenData {
	const axis = buildScreenAxis(range);
	const orderAxis =
		range === 'year'
			? axis
			: (() => {
					const days: string[] = [];
					const end = new Date();
					const start = new Date(end);
					start.setDate(start.getDate() - 6);
					const pad = (n: number) => String(n).padStart(2, '0');
					const cursor = new Date(start);
					while (cursor <= end) {
						days.push(`${pad(cursor.getMonth() + 1)}-${pad(cursor.getDate())}`);
						cursor.setDate(cursor.getDate() + 1);
					}
					return days;
			  })();

	return {
		range,
		axis,
		kpis: {
			todayWeightKg: 0,
			periodWeightTon: 0,
			resourceRate: 0,
			periodValue: 0,
			periodCarbonTon: 0,
			totalWeightTon: 0,
			totalParticipants: 0,
			totalPoints: 0,
			recyclableTon: 0,
			bulkTon: 0,
			elecTon: 0,
			fabricTon: 0,
			yearCarbonTon: 0,
			monthCarbonTon: 0,
			totalCarbonTon: 0,
			greenActions: 0,
			newParticipants: 0,
			participateRate: 0,
			pointsIssued: 0,
			pointsConsumed: 0,
			pointsBalance: 0,
			sorterOnline: 0,
			sorterTotal: 0,
			vehicleOnline: 0,
			vehicleTotal: 0,
			siteOnline: 0,
			siteTotal: 0,
			todayOrders: 0,
			todayAppointments: 0,
			pendingAppointments: 0,
			alerts: 0,
			healthAlerts: 0
		},
		wasteMix: WASTE_ITEMS.map(e => ({
			name: e.name,
			code: e.code,
			weight: 0,
			value: 0,
			count: 0,
			color: WASTE_COLORS[e.code]
		})),
		recycleItems: RECYCLE_NAMES.map((name, i) => ({
			name,
			weight: 0,
			value: 0,
			carbon: 0,
			color: ITEM_COLORS[i]
		})),
		trend: {
			weight: axis.map(() => 0),
			value: axis.map(() => 0),
			carbon: axis.map(() => 0)
		},
		yearCarbon: Array.from({ length: 12 }, () => 0),
		deptRank: [],
		classRank: [],
		majorRank: [],
		pointFlow: { issued: 0, transfer: 0, donate: 0, mall: 0, balance: 0 },
		participateTrend: axis.map(() => 0),
		deliverTrend: axis.map(() => 0),
		ordersByType: {
			axis: orderAxis,
			现场回收: orderAxis.map(() => 0),
			预约上门: orderAxis.map(() => 0),
			大屏投递: orderAxis.map(() => 0),
			活动回收: orderAxis.map(() => 0)
		},
		appointmentFlow: [
			{ name: '预约', value: 0 },
			{ name: '接单', value: 0 },
			{ name: '完成', value: 0 }
		],
		sorterStatus: [
			{ name: '工作中', value: 0, color: '#22c55e' },
			{ name: '休息', value: 0, color: '#f59e0b' },
			{ name: '异常', value: 0, color: '#ef4444' },
			{ name: '离线', value: 0, color: '#94a3b8' }
		],
		vehicles: [],
		sites: [],
		unavailable: []
	};
}
