import { and, eq, ilike, isNull, ne, not, or } from "drizzle-orm";
import { financialAccounts, transactions } from "@/db/schema";
import {
	ACCOUNT_AUTO_INVOICE_NOTE_PREFIX,
	INITIAL_BALANCE_NOTE,
} from "@/shared/lib/accounts/constants";

type DashboardAdminFiltersParams = {
	userId: string;
	adminPayerId: string;
};

type DashboardAdminPeriodFiltersParams = DashboardAdminFiltersParams & {
	period: string;
};

export const buildDashboardAdminFilters = ({
	userId,
	adminPayerId,
}: DashboardAdminFiltersParams) =>
	[
		eq(transactions.userId, userId),
		eq(transactions.payerId, adminPayerId),
	] as const;

export const buildDashboardAdminPeriodFilters = ({
	userId,
	period,
	adminPayerId,
}: DashboardAdminPeriodFiltersParams) =>
	[
		...buildDashboardAdminFilters({ userId, adminPayerId }),
		eq(transactions.period, period),
	] as const;

export const excludeAutoInvoiceEntries = () =>
	or(
		isNull(transactions.note),
		not(ilike(transactions.note, `${ACCOUNT_AUTO_INVOICE_NOTE_PREFIX}%`)),
	);

export const excludeAutoGeneratedEntryNotes = () =>
	or(
		isNull(transactions.note),
		and(
			ne(transactions.note, INITIAL_BALANCE_NOTE),
			not(ilike(transactions.note, `${ACCOUNT_AUTO_INVOICE_NOTE_PREFIX}%`)),
		),
	);

export const excludeInitialBalanceWhenConfigured = () =>
	or(
		ne(transactions.note, INITIAL_BALANCE_NOTE),
		isNull(financialAccounts.excludeInitialBalanceFromIncome),
		eq(financialAccounts.excludeInitialBalanceFromIncome, false),
	);
