export type ServiceOrderStatus =
	'created' |
	'paid' |
	'assigned' |
	'accepted' |
	'rejected' |
	'departed' |
	'arrived' |
	'in_service' |
	'completed' |
	'pending_acceptance' |
	'accepted_by_user' |
	'reviewed' |
	'settled' |
	'cancelled' |
	'exception'

export const SERVICE_ORDER_STATUS_LIST: Array<ServiceOrderStatus> = [
	'created',
	'paid',
	'assigned',
	'accepted',
	'rejected',
	'departed',
	'arrived',
	'in_service',
	'completed',
	'pending_acceptance',
	'accepted_by_user',
	'reviewed',
	'settled',
	'cancelled',
	'exception'
]

export type ServiceOrderAddressSnapshotType = {
	addressId: string
	contactName: string
	contactPhone: string
	province: string
	city: string
	district: string
	detailAddress: string
	fullAddress: string
	latitude: number
	longitude: number
	coordinateType: string
	remark: string
}

export type ServiceOrderServiceSnapshotType = {
	serviceId: string
	serviceName: string
	category: string
	price: number
	durationText: string
	summary: string
	tags: Array<string>
	suitableFor: string
}

export type ServiceOrderTimelineItemType = {
	id: string
	orderId: string
	fromStatus: string
	toStatus: ServiceOrderStatus
	operatorId: string
	operatorRole: string
	remark: string
	createdAt: string
}

export type ServiceExecutionRecordType = {
	id: string
	orderId: string
	assignmentId: string
	checkinTime: string
	checkinLatitude: number
	checkinLongitude: number
	checkinAddress: string
	serviceStartedAt: string
	serviceFinishedAt: string
	actualDurationMinutes: number
	serviceItemsJson: string
	summary: string
	remark: string
	trackPointsJson: string
	createdAt: string
	updatedAt: string
}

export type ServiceEvidenceFileType = {
	id: string
	orderId: string
	executionRecordId: string
	phase: string
	fileType: string
	storagePath: string
	fileUrl: string
	latitude: number
	longitude: number
	capturedAt: string
	createdAt: string
}

export type ServiceReviewType = {
	id: string
	orderId: string
	userId: string
	rating: number
	tags: Array<string>
	content: string
	createdAt: string
}

export type ServiceOrderType = {
	id: string
	orderNo: string
	userId: string
	serviceId: string
	serviceName: string
	serviceSnapshot: ServiceOrderServiceSnapshotType
	serviceAddressId: string
	addressSnapshot: ServiceOrderAddressSnapshotType
	recipientName: string
	recipientPhone: string
	contactName: string
	contactPhone: string
	appointmentTime: string
	remark: string
	status: ServiceOrderStatus
	currentAssignmentId: string
	currentStaffId: string
	acceptedAt: string
	departedAt: string
	arrivedAt: string
	serviceStartedAt: string
	completedAt: string
	pendingAcceptanceAt: string
	acceptedByUserAt: string
	reviewedAt: string
	createdAt: string
	updatedAt: string
	staffName: string
	staffPhone: string
	logs: Array<ServiceOrderTimelineItemType>
	executionRecord: ServiceExecutionRecordType | null
	evidenceFiles: Array<ServiceEvidenceFileType>
	review: ServiceReviewType | null
}

export function getServiceOrderStatusText(status: ServiceOrderStatus): string {
	if (status == 'created') return '待处理'
	if (status == 'paid') return '已支付'
	if (status == 'assigned') return '已派单'
	if (status == 'accepted') return '已接单'
	if (status == 'rejected') return '已拒单'
	if (status == 'departed') return '已出发'
	if (status == 'arrived') return '已到达'
	if (status == 'in_service') return '服务中'
	if (status == 'completed') return '已完成'
	if (status == 'pending_acceptance') return '待验收'
	if (status == 'accepted_by_user') return '已验收'
	if (status == 'reviewed') return '已评价'
	if (status == 'settled') return '已结算'
	if (status == 'cancelled') return '已取消'
	return '异常'
}

export function normalizeServiceOrderStatus(status: string): ServiceOrderStatus {
	if (status == 'ORDER_CREATED') return 'created'
	if (status == 'ORDER_ASSIGNED') return 'assigned'
	if (status == 'ORDER_ACCEPTED') return 'accepted'
	if (status == 'ORDER_REJECTED') return 'rejected'
	if (status == 'ORDER_CHECKED_IN') return 'arrived'
	if (status == 'ORDER_IN_SERVICE') return 'in_service'
	if (status == 'ORDER_COMPLETED' || status == 'ACCEPTANCE_PENDING') return 'pending_acceptance'
	if (status == 'ACCEPTED') return 'accepted_by_user'
	if (status == 'SETTLEMENT_READY' || status == 'ARCHIVED') return 'settled'
	if (status == 'ORDER_CANCELLED') return 'cancelled'
	if (status == 'ORDER_EXCEPTION' || status == 'ACCEPTANCE_REJECTED') return 'exception'
	if (status == 'created' || status == 'submitted') return 'created'
	if (status == 'paid') return 'paid'
	if (status == 'assigned' || status == 'pending_dispatch' || status == 'pending_assignment') return 'assigned'
	if (status == 'accepted' || status == 'pending_accept') return 'accepted'
	if (status == 'rejected') return 'rejected'
	if (status == 'departed' || status == 'waiting_departure' || status == 'on_the_way') return 'departed'
	if (status == 'arrived' || status == 'checked_in') return 'arrived'
	if (status == 'in_service' || status == 'serving') return 'in_service'
	if (status == 'completed') return 'completed'
	if (status == 'pending_acceptance' || status == 'pending_confirm' || status == 'pending_submit') return 'pending_acceptance'
	if (status == 'accepted_by_user') return 'accepted_by_user'
	if (status == 'reviewed') return 'reviewed'
	if (status == 'settled') return 'settled'
	if (status == 'cancelled') return 'cancelled'
	return 'exception'
}