import { platformAdminApi, salespersonApi } from '@/api/businessApi.js';
import * as clinicApi from '@/api/clinicAdmin.js';

const BUSINESS_PACKAGE_ROOT = '/subPackages/sub_business_shared';
const PLATFORM_HOME = '/subPackages/sub_platform_admin/home/index';
const PLATFORM_MY = '/subPackages/sub_platform_admin/my/index';
const SALESPERSON_HOME = '/subPackages/sub_salesperson/home/index';
const SALESPERSON_MY = '/subPackages/sub_salesperson/my/index';

export function getPlatformAdminContext() {
	return {
		mode: 'platform_admin',
		api: platformAdminApi,
		userStorageKey: 'platform_admin_user',
		homePath: PLATFORM_HOME,
		myPath: PLATFORM_MY,
		packageRoot: '/subPackages/sub_platform_admin',
		businessPackageRoot: BUSINESS_PACKAGE_ROOT,
		brandTitle: '平台管理工作台',
		capabilities: {
			canShip: true,
			canRefund: true,
			canStoreFilter: true,
			canWithdraw: true,
			canWithdrawAudit: true,
			canWarehouse: true,
			warehouseType: 'platform',
			canStoreInputAudit: true,
			canDoctorInputAudit: true,
		},
		tabList: [
			{ text: '工作台', iconPath: '/static/tabbar/home.png', selectedIconPath: '/static/tabbar/home-active.png', pagePath: PLATFORM_HOME },
			{ text: '我的', iconPath: '/static/tabbar/my.png', selectedIconPath: '/static/tabbar/my-active.png', pagePath: PLATFORM_MY },
		],
	};
}

export function getSalespersonContext() {
	return {
		mode: 'salesperson',
		api: salespersonApi,
		userStorageKey: 'salesperson_user',
		homePath: SALESPERSON_HOME,
		myPath: SALESPERSON_MY,
		packageRoot: '/subPackages/sub_salesperson',
		businessPackageRoot: BUSINESS_PACKAGE_ROOT,
		brandTitle: '业务工作台',
		capabilities: {
			canShip: false,
			canRefund: false,
			canStoreFilter: true,
			canStoreInput: true,
			canDoctorInput: true,
		},
		tabList: [
			{ text: '工作台', iconPath: '/static/tabbar/home.png', selectedIconPath: '/static/tabbar/home-active.png', pagePath: SALESPERSON_HOME },
			{ text: '我的', iconPath: '/static/tabbar/my.png', selectedIconPath: '/static/tabbar/my-active.png', pagePath: SALESPERSON_MY },
		],
	};
}

export function getClinicAdminContext() {
	return {
		mode: 'clinic_admin',
		api: clinicApi,
		userStorageKey: 'clinic_admin_user',
		homePath: '/subPackages/sub_clinic_admin/home/index',
		myPath: '/subPackages/sub_clinic_admin/my/index',
		packageRoot: '/subPackages/sub_clinic_admin',
		businessPackageRoot: '/subPackages/sub_clinic_admin',
		brandTitle: '诊所管理系统',
		capabilities: {
			canShip: false,
			canRefund: false,
			canStoreFilter: false,
			canWithdraw: true,
			canWithdrawAudit: false,
			canWarehouse: true,
			warehouseType: 'store',
		},
		tabList: [
			{ text: '工作台', iconPath: '/static/tabbar/home.png', selectedIconPath: '/static/tabbar/home-active.png', pagePath: '/subPackages/sub_clinic_admin/home/index' },
			{ text: '我的', iconPath: '/static/tabbar/my.png', selectedIconPath: '/static/tabbar/my-active.png', pagePath: '/subPackages/sub_clinic_admin/my/index' },
		],
	};
}

export function resolveBusinessContext(injected) {
	if (injected && injected.api) return injected;
	const mode = uni.getStorageSync('loginMode');
	if (mode === 'platform_admin') return getPlatformAdminContext();
	if (mode === 'salesperson') return getSalespersonContext();
	return getClinicAdminContext();
}

export function businessPageRoot(ctx) {
	return ctx.businessPackageRoot || ctx.packageRoot;
}

export function pageUrl(ctx, subPath) {
	return `${businessPageRoot(ctx)}/${subPath}`;
}
