/**
 * Permisos de facturas — mantener alineado con
 * backend/api/v1/modules/a76/invoices/permission_map.py
 */
import type { User } from '$lib/auth';
import { userHasPermission } from '$lib/auth';

/** Permisos *.view definidos en seed (facturas); incluye genérico exp. */
export const INVOICE_VIEW_PERMISSION_CODES: string[] = [
	'invoice.imp.tem.view',
	'invoice.imp.def.view',
	'invoice.imp.cm.view',
	'invoice.imp.cr.view',
	'invoice.imp.rep.view',
	'invoice.exp.rep.view',
	'invoice.exp.donac.view',
	'invoice.exp.view'
];

/** Permisos *.create del seed de facturas (para “puede crear algo”). */
const INVOICE_ANY_CREATE_CODES: string[] = [
	'invoice.imp.tem.create',
	'invoice.imp.def.create',
	'invoice.imp.cm.create',
	'invoice.imp.cr.create',
	'invoice.imp.rep.create',
	'invoice.exp.create',
	'invoice.exp.rep.create',
	'invoice.exp.donac.create'
];

export function getInvoicePermissionBase(operationType: string, invoiceType: string): string {
	const op = (operationType || '').toLowerCase().split('.').pop() || '';
	const inv = (invoiceType || '').toUpperCase();

	if (op === 'imp') {
		if (inv === 'TEM') return 'invoice.imp.tem';
		if (inv === 'DEF') return 'invoice.imp.def';
		if (inv === 'MEX') return 'invoice.imp.cm';
		if (inv === 'CR') return 'invoice.imp.cr';
		if (inv === 'REP') return 'invoice.imp.rep';
		return 'invoice.imp.tem';
	}
	if (op === 'exp') {
		if (inv === 'REPAR') return 'invoice.exp.rep';
		if (inv === 'DONAC') return 'invoice.exp.donac';
		return 'invoice.exp';
	}
	return 'invoice.imp.tem';
}

export function userHasInvoiceAction(
	user: User | null,
	operationType: string,
	invoiceType: string,
	action: 'view' | 'create' | 'edit' | 'delete' | 'process'
): boolean {
	if (!operationType || !invoiceType) return false;
	const base = getInvoicePermissionBase(operationType, invoiceType);
	return userHasPermission(user, `${base}.${action}`);
}

/**
 * Acceso a la lista de facturas según filtros de URL.
 * Sin operation_type/invoice_type: requiere al menos un permiso *.view de facturas.
 */
export function userCanViewInvoiceListPage(
	user: User | null,
	operationType: string,
	invoiceType: string
): boolean {
	if (!user) return false;
	const ot = (operationType || '').trim();
	const it = (invoiceType || '').trim();

	if (ot && it) {
		const base = getInvoicePermissionBase(ot, it);
		return userHasPermission(user, `${base}.view`);
	}

	if (ot && !it) {
		const prefix = ot === 'imp' ? 'invoice.imp.' : ot === 'exp' ? 'invoice.exp.' : '';
		if (!prefix) {
			return INVOICE_VIEW_PERMISSION_CODES.some((code) => userHasPermission(user, code));
		}
		return INVOICE_VIEW_PERMISSION_CODES.some(
			(code) => code.startsWith(prefix) && code.endsWith('.view') && userHasPermission(user, code)
		);
	}

	return INVOICE_VIEW_PERMISSION_CODES.some((code) => userHasPermission(user, code));
}

export function userCanCreateInvoiceForListFilters(
	user: User | null,
	operationType: string,
	invoiceType: string
): boolean {
	if (!user) return false;
	const ot = (operationType || '').trim();
	const it = (invoiceType || '').trim();

	if (ot === 'imp' && it) {
		return userHasInvoiceAction(user, ot, it, 'create');
	}
	if (ot === 'exp') {
		if (it) {
			return userHasInvoiceAction(user, ot, it, 'create');
		}
		return userHasPermission(user, 'invoice.exp.create');
	}

	return INVOICE_ANY_CREATE_CODES.some((code) => userHasPermission(user, code));
}
