refactor(core): move app para src e padroniza estrutura

This commit is contained in:
Felipe Coutinho
2026-03-12 19:22:50 +00:00
parent d92e70f1b9
commit b0fbb1062a
567 changed files with 8981 additions and 5014 deletions

View File

@@ -0,0 +1,871 @@
"use server";
import { anthropic } from "@ai-sdk/anthropic";
import { google } from "@ai-sdk/google";
import { openai } from "@ai-sdk/openai";
import { createOpenRouter } from "@openrouter/ai-sdk-provider";
import { generateObject } from "ai";
import { getDay } from "date-fns";
import { and, eq, isNull, ne, or, sql } from "drizzle-orm";
import {
cartoes,
categorias,
contas,
insightsSalvos,
lancamentos,
orcamentos,
pagadores,
} from "@/db/schema";
import { ACCOUNT_AUTO_INVOICE_NOTE_PREFIX } from "@/shared/lib/accounts/constants";
import { getUser } from "@/shared/lib/auth/server";
import { db } from "@/shared/lib/db";
import { PAGADOR_ROLE_ADMIN } from "@/shared/lib/payers/constants";
import {
type InsightsResponse,
InsightsResponseSchema,
} from "@/shared/lib/schemas/insights";
import { getPreviousPeriod } from "@/shared/utils/period";
import { AVAILABLE_MODELS, INSIGHTS_SYSTEM_PROMPT } from "./constants";
const TRANSFERENCIA = "Transferência";
type ActionResult<T> =
| { success: true; data: T }
| { success: false; error: string };
/**
* Função auxiliar para converter valores numéricos
*/
const toNumber = (value: unknown): number => {
if (typeof value === "number") return value;
if (typeof value === "string") {
const parsed = Number(value);
return Number.isNaN(parsed) ? 0 : parsed;
}
return 0;
};
/**
* Agrega dados financeiros do mês para análise
*/
async function aggregateMonthData(userId: string, period: string) {
const previousPeriod = getPreviousPeriod(period);
const twoMonthsAgo = getPreviousPeriod(previousPeriod);
const threeMonthsAgo = getPreviousPeriod(twoMonthsAgo);
// Buscar métricas de receitas e despesas dos últimos 3 meses
const [
currentPeriodRows,
previousPeriodRows,
twoMonthsAgoRows,
threeMonthsAgoRows,
] = await Promise.all([
db
.select({
transactionType: lancamentos.transactionType,
totalAmount: sql<number>`coalesce(sum(${lancamentos.amount}), 0)`,
})
.from(lancamentos)
.innerJoin(pagadores, eq(lancamentos.pagadorId, pagadores.id))
.where(
and(
eq(lancamentos.userId, userId),
eq(lancamentos.period, period),
eq(pagadores.role, PAGADOR_ROLE_ADMIN),
ne(lancamentos.transactionType, TRANSFERENCIA),
or(
isNull(lancamentos.note),
sql`${
lancamentos.note
} NOT LIKE ${`${ACCOUNT_AUTO_INVOICE_NOTE_PREFIX}%`}`,
),
),
)
.groupBy(lancamentos.transactionType),
db
.select({
transactionType: lancamentos.transactionType,
totalAmount: sql<number>`coalesce(sum(${lancamentos.amount}), 0)`,
})
.from(lancamentos)
.innerJoin(pagadores, eq(lancamentos.pagadorId, pagadores.id))
.where(
and(
eq(lancamentos.userId, userId),
eq(lancamentos.period, previousPeriod),
eq(pagadores.role, PAGADOR_ROLE_ADMIN),
ne(lancamentos.transactionType, TRANSFERENCIA),
or(
isNull(lancamentos.note),
sql`${
lancamentos.note
} NOT LIKE ${`${ACCOUNT_AUTO_INVOICE_NOTE_PREFIX}%`}`,
),
),
)
.groupBy(lancamentos.transactionType),
db
.select({
transactionType: lancamentos.transactionType,
totalAmount: sql<number>`coalesce(sum(${lancamentos.amount}), 0)`,
})
.from(lancamentos)
.innerJoin(pagadores, eq(lancamentos.pagadorId, pagadores.id))
.where(
and(
eq(lancamentos.userId, userId),
eq(lancamentos.period, twoMonthsAgo),
eq(pagadores.role, PAGADOR_ROLE_ADMIN),
ne(lancamentos.transactionType, TRANSFERENCIA),
or(
isNull(lancamentos.note),
sql`${
lancamentos.note
} NOT LIKE ${`${ACCOUNT_AUTO_INVOICE_NOTE_PREFIX}%`}`,
),
),
)
.groupBy(lancamentos.transactionType),
db
.select({
transactionType: lancamentos.transactionType,
totalAmount: sql<number>`coalesce(sum(${lancamentos.amount}), 0)`,
})
.from(lancamentos)
.innerJoin(pagadores, eq(lancamentos.pagadorId, pagadores.id))
.where(
and(
eq(lancamentos.userId, userId),
eq(lancamentos.period, threeMonthsAgo),
eq(pagadores.role, PAGADOR_ROLE_ADMIN),
ne(lancamentos.transactionType, TRANSFERENCIA),
or(
isNull(lancamentos.note),
sql`${
lancamentos.note
} NOT LIKE ${`${ACCOUNT_AUTO_INVOICE_NOTE_PREFIX}%`}`,
),
),
)
.groupBy(lancamentos.transactionType),
]);
// Calcular totais dos últimos 3 meses
let currentIncome = 0;
let currentExpense = 0;
let previousIncome = 0;
let previousExpense = 0;
let twoMonthsAgoIncome = 0;
let twoMonthsAgoExpense = 0;
let threeMonthsAgoIncome = 0;
let threeMonthsAgoExpense = 0;
for (const row of currentPeriodRows) {
const amount = Math.abs(toNumber(row.totalAmount));
if (row.transactionType === "Receita") currentIncome += amount;
else if (row.transactionType === "Despesa") currentExpense += amount;
}
for (const row of previousPeriodRows) {
const amount = Math.abs(toNumber(row.totalAmount));
if (row.transactionType === "Receita") previousIncome += amount;
else if (row.transactionType === "Despesa") previousExpense += amount;
}
for (const row of twoMonthsAgoRows) {
const amount = Math.abs(toNumber(row.totalAmount));
if (row.transactionType === "Receita") twoMonthsAgoIncome += amount;
else if (row.transactionType === "Despesa") twoMonthsAgoExpense += amount;
}
for (const row of threeMonthsAgoRows) {
const amount = Math.abs(toNumber(row.totalAmount));
if (row.transactionType === "Receita") threeMonthsAgoIncome += amount;
else if (row.transactionType === "Despesa") threeMonthsAgoExpense += amount;
}
// Buscar despesas por categoria (top 5)
const expensesByCategory = await db
.select({
categoryName: categorias.name,
total: sql<number>`coalesce(sum(${lancamentos.amount}), 0)`,
})
.from(lancamentos)
.innerJoin(pagadores, eq(lancamentos.pagadorId, pagadores.id))
.innerJoin(categorias, eq(lancamentos.categoriaId, categorias.id))
.where(
and(
eq(lancamentos.userId, userId),
eq(lancamentos.period, period),
eq(lancamentos.transactionType, "Despesa"),
eq(pagadores.role, PAGADOR_ROLE_ADMIN),
eq(categorias.type, "despesa"),
or(
isNull(lancamentos.note),
sql`${
lancamentos.note
} NOT LIKE ${`${ACCOUNT_AUTO_INVOICE_NOTE_PREFIX}%`}`,
),
),
)
.groupBy(categorias.name)
.orderBy(sql`sum(${lancamentos.amount}) ASC`)
.limit(5);
// Buscar orçamentos e uso
const budgetsData = await db
.select({
categoryName: categorias.name,
budgetAmount: orcamentos.amount,
spent: sql<number>`coalesce(sum(${lancamentos.amount}), 0)`,
})
.from(orcamentos)
.innerJoin(categorias, eq(orcamentos.categoriaId, categorias.id))
.leftJoin(
lancamentos,
and(
eq(lancamentos.categoriaId, categorias.id),
eq(lancamentos.period, period),
eq(lancamentos.userId, userId),
eq(lancamentos.transactionType, "Despesa"),
),
)
.where(and(eq(orcamentos.userId, userId), eq(orcamentos.period, period)))
.groupBy(categorias.name, orcamentos.amount);
// Buscar métricas de cartões
const cardsData = await db
.select({
totalLimit: sql<number>`coalesce(sum(${cartoes.limit}), 0)`,
cardCount: sql<number>`count(*)`,
})
.from(cartoes)
.where(and(eq(cartoes.userId, userId), eq(cartoes.status, "ativo")));
// Buscar saldo total das contas
const accountsData = await db
.select({
totalBalance: sql<number>`coalesce(sum(${contas.initialBalance}), 0)`,
accountCount: sql<number>`count(*)`,
})
.from(contas)
.where(
and(
eq(contas.userId, userId),
eq(contas.status, "ativa"),
eq(contas.excludeFromBalance, false),
),
);
// Calcular ticket médio das transações
const avgTicketData = await db
.select({
avgAmount: sql<number>`coalesce(avg(abs(${lancamentos.amount})), 0)`,
transactionCount: sql<number>`count(*)`,
})
.from(lancamentos)
.innerJoin(pagadores, eq(lancamentos.pagadorId, pagadores.id))
.where(
and(
eq(lancamentos.userId, userId),
eq(lancamentos.period, period),
eq(pagadores.role, PAGADOR_ROLE_ADMIN),
ne(lancamentos.transactionType, TRANSFERENCIA),
),
);
// Buscar gastos por dia da semana
const dayOfWeekSpending = await db
.select({
purchaseDate: lancamentos.purchaseDate,
amount: lancamentos.amount,
})
.from(lancamentos)
.innerJoin(pagadores, eq(lancamentos.pagadorId, pagadores.id))
.where(
and(
eq(lancamentos.userId, userId),
eq(lancamentos.period, period),
eq(lancamentos.transactionType, "Despesa"),
eq(pagadores.role, PAGADOR_ROLE_ADMIN),
),
);
// Agregar por dia da semana
const dayTotals = new Map<number, number>();
for (const row of dayOfWeekSpending) {
if (!row.purchaseDate) continue;
const dayOfWeek = getDay(new Date(row.purchaseDate));
const current = dayTotals.get(dayOfWeek) ?? 0;
dayTotals.set(dayOfWeek, current + Math.abs(toNumber(row.amount)));
}
// Buscar métodos de pagamento (agregado)
const paymentMethodsData = await db
.select({
paymentMethod: lancamentos.paymentMethod,
total: sql<number>`coalesce(sum(abs(${lancamentos.amount})), 0)`,
})
.from(lancamentos)
.innerJoin(pagadores, eq(lancamentos.pagadorId, pagadores.id))
.where(
and(
eq(lancamentos.userId, userId),
eq(lancamentos.period, period),
eq(lancamentos.transactionType, "Despesa"),
eq(pagadores.role, PAGADOR_ROLE_ADMIN),
),
)
.groupBy(lancamentos.paymentMethod);
// Buscar transações dos últimos 3 meses para análise de recorrência
const last3MonthsTransactions = await db
.select({
name: lancamentos.name,
amount: lancamentos.amount,
period: lancamentos.period,
condition: lancamentos.condition,
installmentCount: lancamentos.installmentCount,
currentInstallment: lancamentos.currentInstallment,
categoryName: categorias.name,
})
.from(lancamentos)
.innerJoin(pagadores, eq(lancamentos.pagadorId, pagadores.id))
.leftJoin(categorias, eq(lancamentos.categoriaId, categorias.id))
.where(
and(
eq(lancamentos.userId, userId),
sql`${lancamentos.period} IN (${period}, ${previousPeriod}, ${twoMonthsAgo})`,
eq(lancamentos.transactionType, "Despesa"),
eq(pagadores.role, PAGADOR_ROLE_ADMIN),
ne(lancamentos.transactionType, TRANSFERENCIA),
),
)
.orderBy(lancamentos.name);
// Análise de recorrência
const transactionsByName = new Map<
string,
Array<{ period: string; amount: number }>
>();
for (const tx of last3MonthsTransactions) {
const key = tx.name.toLowerCase().trim();
if (!transactionsByName.has(key)) {
transactionsByName.set(key, []);
}
const transactions = transactionsByName.get(key);
if (transactions) {
transactions.push({
period: tx.period,
amount: Math.abs(toNumber(tx.amount)),
});
}
}
// Identificar gastos recorrentes (aparece em 2+ meses com valor similar)
const recurringExpenses: Array<{
name: string;
avgAmount: number;
frequency: number;
}> = [];
let totalRecurring = 0;
for (const [name, occurrences] of transactionsByName.entries()) {
if (occurrences.length >= 2) {
const amounts = occurrences.map((o) => o.amount);
const avgAmount =
amounts.reduce((sum, amt) => sum + amt, 0) / amounts.length;
const maxDiff = Math.max(...amounts) - Math.min(...amounts);
// Considerar recorrente se variação <= 20% da média
if (maxDiff <= avgAmount * 0.2) {
recurringExpenses.push({
name,
avgAmount,
frequency: occurrences.length,
});
// Somar apenas os do mês atual
const currentMonthOccurrence = occurrences.find(
(o) => o.period === period,
);
if (currentMonthOccurrence) {
totalRecurring += currentMonthOccurrence.amount;
}
}
}
}
// Análise de gastos parcelados
const installmentTransactions = last3MonthsTransactions.filter(
(tx) =>
tx.condition === "Parcelado" &&
tx.installmentCount &&
tx.installmentCount > 1,
);
const installmentData = installmentTransactions
.filter((tx) => tx.period === period)
.map((tx) => ({
name: tx.name,
currentInstallment: tx.currentInstallment ?? 1,
totalInstallments: tx.installmentCount ?? 1,
amount: Math.abs(toNumber(tx.amount)),
category: tx.categoryName ?? "Outros",
}));
const totalInstallmentAmount = installmentData.reduce(
(sum, tx) => sum + tx.amount,
0,
);
const futureCommitment = installmentData.reduce((sum, tx) => {
const remaining = tx.totalInstallments - tx.currentInstallment;
return sum + tx.amount * remaining;
}, 0);
// Montar dados agregados e anonimizados
const aggregatedData = {
month: period,
totalIncome: currentIncome,
totalExpense: currentExpense,
balance: currentIncome - currentExpense,
// Tendência de 3 meses
threeMonthTrend: {
periods: [threeMonthsAgo, twoMonthsAgo, previousPeriod, period],
incomes: [
threeMonthsAgoIncome,
twoMonthsAgoIncome,
previousIncome,
currentIncome,
],
expenses: [
threeMonthsAgoExpense,
twoMonthsAgoExpense,
previousExpense,
currentExpense,
],
avgIncome:
(threeMonthsAgoIncome +
twoMonthsAgoIncome +
previousIncome +
currentIncome) /
4,
avgExpense:
(threeMonthsAgoExpense +
twoMonthsAgoExpense +
previousExpense +
currentExpense) /
4,
trend:
currentExpense > previousExpense &&
previousExpense > twoMonthsAgoExpense
? "crescente"
: currentExpense < previousExpense &&
previousExpense < twoMonthsAgoExpense
? "decrescente"
: "estável",
},
previousMonthIncome: previousIncome,
previousMonthExpense: previousExpense,
monthOverMonthIncomeChange:
Math.abs(previousIncome) > 0.01
? ((currentIncome - previousIncome) / Math.abs(previousIncome)) * 100
: 0,
monthOverMonthExpenseChange:
Math.abs(previousExpense) > 0.01
? ((currentExpense - previousExpense) / Math.abs(previousExpense)) * 100
: 0,
savingsRate:
currentIncome > 0.01
? ((currentIncome - currentExpense) / currentIncome) * 100
: 0,
topExpenseCategories: expensesByCategory.map(
(cat: { categoryName: string; total: unknown }) => ({
category: cat.categoryName,
amount: Math.abs(toNumber(cat.total)),
percentageOfTotal:
currentExpense > 0
? (Math.abs(toNumber(cat.total)) / currentExpense) * 100
: 0,
}),
),
budgets: budgetsData.map(
(b: { categoryName: string; budgetAmount: unknown; spent: unknown }) => ({
category: b.categoryName,
budgetAmount: toNumber(b.budgetAmount),
spent: Math.abs(toNumber(b.spent)),
usagePercentage:
toNumber(b.budgetAmount) > 0
? (Math.abs(toNumber(b.spent)) / toNumber(b.budgetAmount)) * 100
: 0,
}),
),
creditCards: {
totalLimit: toNumber(cardsData[0]?.totalLimit ?? 0),
cardCount: toNumber(cardsData[0]?.cardCount ?? 0),
},
accounts: {
totalBalance: toNumber(accountsData[0]?.totalBalance ?? 0),
accountCount: toNumber(accountsData[0]?.accountCount ?? 0),
},
avgTicket: toNumber(avgTicketData[0]?.avgAmount ?? 0),
transactionCount: toNumber(avgTicketData[0]?.transactionCount ?? 0),
dayOfWeekSpending: Array.from(dayTotals.entries()).map(([day, total]) => ({
dayOfWeek:
["Dom", "Seg", "Ter", "Qua", "Qui", "Sex", "Sáb"][day] ?? "N/A",
total,
})),
paymentMethodsBreakdown: paymentMethodsData.map(
(pm: { paymentMethod: string | null; total: unknown }) => ({
method: pm.paymentMethod,
total: toNumber(pm.total),
percentage:
currentExpense > 0 ? (toNumber(pm.total) / currentExpense) * 100 : 0,
}),
),
// Análise de recorrência
recurringExpenses: {
count: recurringExpenses.length,
total: totalRecurring,
percentageOfTotal:
currentExpense > 0 ? (totalRecurring / currentExpense) * 100 : 0,
topRecurring: recurringExpenses
.sort((a, b) => b.avgAmount - a.avgAmount)
.slice(0, 5)
.map((r) => ({
name: r.name,
avgAmount: r.avgAmount,
frequency: r.frequency,
})),
predictability:
currentExpense > 0 ? (totalRecurring / currentExpense) * 100 : 0,
},
// Análise de parcelamentos
installments: {
currentMonthInstallments: installmentData.length,
totalInstallmentAmount,
percentageOfExpenses:
currentExpense > 0
? (totalInstallmentAmount / currentExpense) * 100
: 0,
futureCommitment,
topInstallments: installmentData
.sort((a, b) => b.amount - a.amount)
.slice(0, 5)
.map((i) => ({
name: i.name,
current: i.currentInstallment,
total: i.totalInstallments,
amount: i.amount,
category: i.category,
remaining: i.totalInstallments - i.currentInstallment,
})),
},
};
return aggregatedData;
}
/**
* Gera insights usando IA
*/
export async function generateInsightsAction(
period: string,
modelId: string,
): Promise<ActionResult<InsightsResponse>> {
try {
const user = await getUser();
// Validar modelo - verificar se existe na lista ou se é um modelo customizado
const selectedModel = AVAILABLE_MODELS.find((m) => m.id === modelId);
// Se não encontrou na lista e não tem "/" (formato OpenRouter), é inválido
const isOpenRouterFormat = /^[a-zA-Z0-9_-]+\/[a-zA-Z0-9._-]+$/.test(
modelId,
);
if (!selectedModel && !isOpenRouterFormat) {
return {
success: false,
error: "Modelo inválido.",
};
}
// Agregar dados
const aggregatedData = await aggregateMonthData(user.id, period);
// Selecionar provider
let model: ReturnType<typeof google>;
// Se o modelo tem "/" é OpenRouter (formato: provider/model)
if (isOpenRouterFormat && !selectedModel) {
const apiKey = process.env.OPENROUTER_API_KEY;
if (!apiKey) {
return {
success: false,
error:
"OPENROUTER_API_KEY não configurada. Adicione a chave no arquivo .env",
};
}
const openrouter = createOpenRouter({
apiKey,
});
model = openrouter.chat(modelId);
} else if (selectedModel?.provider === "openai") {
model = openai(modelId);
} else if (selectedModel?.provider === "anthropic") {
model = anthropic(modelId);
} else if (selectedModel?.provider === "google") {
model = google(modelId);
} else {
return {
success: false,
error: "Provider de modelo não suportado.",
};
}
// Chamar AI SDK
const result = await generateObject({
model,
schema: InsightsResponseSchema,
system: INSIGHTS_SYSTEM_PROMPT,
prompt: `Analise os seguintes dados financeiros agregados do período ${period}.
Dados agregados:
${JSON.stringify(aggregatedData, null, 2)}
DADOS IMPORTANTES PARA SUA ANÁLISE:
**Tendência de 3 meses:**
- Os dados incluem tendência dos últimos 3 meses (threeMonthTrend)
- Use isso para identificar padrões crescentes, decrescentes ou estáveis
- Compare o mês atual com a média dos 3 meses
**Análise de Recorrência:**
- Gastos recorrentes representam ${aggregatedData.recurringExpenses.percentageOfTotal.toFixed(1)}% das despesas
- ${aggregatedData.recurringExpenses.count} gastos identificados como recorrentes
- Use isso para avaliar previsibilidade e oportunidades de otimização
**Gastos Parcelados:**
- ${aggregatedData.installments.currentMonthInstallments} parcelas ativas no mês
- Comprometimento futuro de R$ ${aggregatedData.installments.futureCommitment.toFixed(2)}
- Use isso para alertas sobre comprometimento de renda futura
Organize suas observações nas 4 categorias especificadas no prompt do sistema:
1. Comportamentos Observados (behaviors): 3-6 itens
2. Gatilhos de Consumo (triggers): 3-6 itens
3. Recomendações Práticas (recommendations): 3-6 itens
4. Melhorias Sugeridas (improvements): 3-6 itens
Cada item deve ser conciso, direto e acionável. Use os novos dados para dar contexto temporal e identificar padrões mais profundos.
Responda APENAS com um JSON válido seguindo exatamente o schema especificado.`,
});
// Validar resposta
const validatedData = InsightsResponseSchema.parse(result.object);
return {
success: true,
data: validatedData,
};
} catch (error) {
console.error("Error generating insights:", error);
return {
success: false,
error: "Erro ao gerar insights. Tente novamente.",
};
}
}
/**
* Salva insights gerados no banco de dados
*/
export async function saveInsightsAction(
period: string,
modelId: string,
data: InsightsResponse,
): Promise<ActionResult<{ id: string; createdAt: Date }>> {
try {
const user = await getUser();
// Verificar se já existe um insight salvo para este período
const existing = await db
.select()
.from(insightsSalvos)
.where(
and(
eq(insightsSalvos.userId, user.id),
eq(insightsSalvos.period, period),
),
)
.limit(1);
if (existing.length > 0) {
// Atualizar existente
const updated = await db
.update(insightsSalvos)
.set({
modelId,
data: JSON.stringify(data),
updatedAt: new Date(),
})
.where(
and(
eq(insightsSalvos.userId, user.id),
eq(insightsSalvos.period, period),
),
)
.returning({
id: insightsSalvos.id,
createdAt: insightsSalvos.createdAt,
});
const updatedRecord = updated[0];
if (!updatedRecord) {
return {
success: false,
error: "Falha ao atualizar a análise. Tente novamente.",
};
}
return {
success: true,
data: {
id: updatedRecord.id,
createdAt: updatedRecord.createdAt,
},
};
}
// Criar novo
const result = await db
.insert(insightsSalvos)
.values({
userId: user.id,
period,
modelId,
data: JSON.stringify(data),
})
.returning({
id: insightsSalvos.id,
createdAt: insightsSalvos.createdAt,
});
const insertedRecord = result[0];
if (!insertedRecord) {
return {
success: false,
error: "Falha ao salvar a análise. Tente novamente.",
};
}
return {
success: true,
data: {
id: insertedRecord.id,
createdAt: insertedRecord.createdAt,
},
};
} catch (error) {
console.error("Error saving insights:", error);
return {
success: false,
error: "Erro ao salvar análise. Tente novamente.",
};
}
}
/**
* Carrega insights salvos do banco de dados
*/
export async function loadSavedInsightsAction(period: string): Promise<
ActionResult<{
insights: InsightsResponse;
modelId: string;
createdAt: Date;
} | null>
> {
try {
const user = await getUser();
const result = await db
.select()
.from(insightsSalvos)
.where(
and(
eq(insightsSalvos.userId, user.id),
eq(insightsSalvos.period, period),
),
)
.limit(1);
if (result.length === 0) {
return {
success: true,
data: null,
};
}
const saved = result[0];
if (!saved) {
return {
success: true,
data: null,
};
}
const insights = InsightsResponseSchema.parse(JSON.parse(saved.data));
return {
success: true,
data: {
insights,
modelId: saved.modelId,
createdAt: saved.createdAt,
},
};
} catch (error) {
console.error("Error loading saved insights:", error);
return {
success: false,
error: "Erro ao carregar análise salva. Tente novamente.",
};
}
}
/**
* Remove insights salvos do banco de dados
*/
export async function deleteSavedInsightsAction(
period: string,
): Promise<ActionResult<void>> {
try {
const user = await getUser();
await db
.delete(insightsSalvos)
.where(
and(
eq(insightsSalvos.userId, user.id),
eq(insightsSalvos.period, period),
),
);
return {
success: true,
data: undefined,
};
} catch (error) {
console.error("Error deleting saved insights:", error);
return {
success: false,
error: "Erro ao remover análise. Tente novamente.",
};
}
}

View File

@@ -0,0 +1,109 @@
import {
type RemixiconComponentType,
RiChatAi3Line,
RiEyeLine,
RiFlashlightLine,
RiLightbulbLine,
RiRocketLine,
} from "@remixicon/react";
import {
Card,
CardContent,
CardHeader,
CardTitle,
} from "@/shared/components/ui/card";
import type {
InsightCategoryId,
InsightsResponse,
} from "@/shared/lib/schemas/insights";
import { INSIGHT_CATEGORIES } from "@/shared/lib/schemas/insights";
import { displayPeriod } from "@/shared/utils/period";
import { cn } from "@/shared/utils/ui";
interface InsightsGridProps {
insights: InsightsResponse;
}
const CATEGORY_ICONS: Record<InsightCategoryId, RemixiconComponentType> = {
behaviors: RiEyeLine,
triggers: RiFlashlightLine,
recommendations: RiLightbulbLine,
improvements: RiRocketLine,
};
const CATEGORY_COLORS: Record<
InsightCategoryId,
{ titleText: string; chatAiIcon: string }
> = {
behaviors: {
titleText: "text-orange-700 dark:text-orange-400",
chatAiIcon: "text-orange-600 dark:text-orange-400",
},
triggers: {
titleText: "text-amber-700 dark:text-amber-400 ",
chatAiIcon: "text-amber-600 dark:text-amber-400",
},
recommendations: {
titleText: "text-sky-700 dark:text-sky-400",
chatAiIcon: "text-sky-600 dark:text-sky-400",
},
improvements: {
titleText: "text-emerald-700 dark:text-emerald-400",
chatAiIcon: "text-emerald-600 dark:text-emerald-400",
},
};
export function InsightsGrid({ insights }: InsightsGridProps) {
const formattedPeriod = displayPeriod(insights.month);
return (
<div className="space-y-6">
<div className="space-y-2 px-1 text-muted-foreground">
<p>
No período selecionado ({formattedPeriod}), identificamos os
principais comportamentos e gatilhos que impactaram seu padrão de
consumo.
</p>
<p>Segue um panorama prático com recomendações acionáveis.</p>
</div>
{/* Grid de Cards */}
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
{insights.categories.map((categoryData) => {
const categoryConfig = INSIGHT_CATEGORIES[categoryData.category];
const colors = CATEGORY_COLORS[categoryData.category];
const Icon = CATEGORY_ICONS[categoryData.category];
return (
<Card
key={categoryData.category}
className="relative overflow-hidden"
>
<CardHeader>
<div className="flex items-center gap-2">
<Icon className={cn("size-5", colors.chatAiIcon)} />
<CardTitle className={cn("font-semibold", colors.titleText)}>
{categoryConfig.title}
</CardTitle>
</div>
</CardHeader>
<CardContent>
{categoryData.items.map((item, index) => (
<div
key={index}
className="flex flex-1 border-b border-dashed py-2.5 gap-2 items-start last:border-0"
>
<RiChatAi3Line
className={cn("size-4 shrink-0", colors.chatAiIcon)}
/>
<span className="text-sm">{item.text}</span>
</div>
))}
</CardContent>
</Card>
);
})}
</div>
</div>
);
}

View File

@@ -0,0 +1,280 @@
"use client";
import {
RiAlertLine,
RiDeleteBinLine,
RiSaveLine,
RiSparklingLine,
} from "@remixicon/react";
import { format } from "date-fns";
import { ptBR } from "date-fns/locale";
import { useEffect, useState, useTransition } from "react";
import { toast } from "sonner";
import {
deleteSavedInsightsAction,
generateInsightsAction,
loadSavedInsightsAction,
saveInsightsAction,
} from "@/features/insights/actions";
import { DEFAULT_MODEL } from "@/features/insights/constants";
import { EmptyState } from "@/shared/components/empty-state";
import { Alert, AlertDescription } from "@/shared/components/ui/alert";
import { Button } from "@/shared/components/ui/button";
import { Card, CardContent, CardHeader } from "@/shared/components/ui/card";
import { Skeleton } from "@/shared/components/ui/skeleton";
import type { InsightsResponse } from "@/shared/lib/schemas/insights";
import { InsightsGrid } from "./insights-grid";
import { ModelSelector } from "./model-selector";
interface InsightsPageProps {
period: string;
onAnalyze?: () => void;
}
export function InsightsPage({ period, onAnalyze }: InsightsPageProps) {
const [selectedModel, setSelectedModel] = useState(DEFAULT_MODEL);
const [insights, setInsights] = useState<InsightsResponse | null>(null);
const [isPending, startTransition] = useTransition();
const [isSaving, startSaveTransition] = useTransition();
const [error, setError] = useState<string | null>(null);
const [isSaved, setIsSaved] = useState(false);
const [savedDate, setSavedDate] = useState<Date | null>(null);
const [isLoading, setIsLoading] = useState(true);
// Carregar insights salvos ao montar o componente
useEffect(() => {
const loadSaved = async () => {
try {
const result = await loadSavedInsightsAction(period);
if (result.success && result.data) {
setInsights(result.data.insights);
setSelectedModel(result.data.modelId);
setIsSaved(true);
setSavedDate(result.data.createdAt);
}
} catch (err) {
console.error("Error loading saved insights:", err);
} finally {
setIsLoading(false);
}
};
loadSaved();
}, [period]);
const handleAnalyze = () => {
setError(null);
setIsSaved(false);
setSavedDate(null);
onAnalyze?.();
startTransition(async () => {
try {
const result = await generateInsightsAction(period, selectedModel);
if (result.success) {
setInsights(result.data);
toast.success("Insights gerados com sucesso!");
} else {
setError(result.error);
toast.error(result.error);
}
} catch (err) {
const errorMessage = "Erro inesperado ao gerar insights.";
setError(errorMessage);
toast.error(errorMessage);
console.error("Error generating insights:", err);
}
});
};
const handleSave = () => {
if (!insights) return;
startSaveTransition(async () => {
try {
const result = await saveInsightsAction(
period,
selectedModel,
insights,
);
if (result.success) {
setIsSaved(true);
setSavedDate(result.data.createdAt);
toast.success("Análise salva com sucesso!");
} else {
toast.error(result.error);
}
} catch (err) {
toast.error("Erro ao salvar análise.");
console.error("Error saving insights:", err);
}
});
};
const handleDelete = () => {
startSaveTransition(async () => {
try {
const result = await deleteSavedInsightsAction(period);
if (result.success) {
setIsSaved(false);
setSavedDate(null);
toast.success("Análise removida com sucesso!");
} else {
toast.error(result.error);
}
} catch (err) {
toast.error("Erro ao remover análise.");
console.error("Error deleting insights:", err);
}
});
};
return (
<div className="flex flex-col gap-6">
{/* Privacy Warning */}
<Alert className="border-none">
<RiAlertLine className="size-4" color="red" />
<AlertDescription className="text-sm text-card-foreground">
<strong>Aviso de privacidade:</strong> Ao gerar insights, seus dados
financeiros serão enviados para o provedor de IA selecionado
(Anthropic, OpenAI, Google ou OpenRouter) para processamento.
Certifique-se de que você confia no provedor escolhido antes de
prosseguir.
</AlertDescription>
</Alert>
{/* Model Selector */}
<ModelSelector
value={selectedModel}
onValueChange={setSelectedModel}
disabled={isPending}
/>
{/* Analyze Button */}
<div className="flex items-center gap-3 flex-wrap">
<Button
onClick={handleAnalyze}
disabled={isPending || isLoading}
className="bg-linear-to-r from-primary to-violet-500 dark:from-primary-dark dark:to-emerald-600"
>
<RiSparklingLine className="mr-2 size-5" aria-hidden="true" />
{isPending ? "Analisando..." : "Gerar análise inteligente"}
</Button>
{insights && !error && (
<Button
onClick={isSaved ? handleDelete : handleSave}
disabled={isSaving || isPending || isLoading}
variant={isSaved ? "destructive" : "outline"}
>
{isSaved ? (
<>
<RiDeleteBinLine className="mr-2 size-4" />
{isSaving ? "Removendo..." : "Remover análise"}
</>
) : (
<>
<RiSaveLine className="mr-2 size-4" />
{isSaving ? "Salvando..." : "Salvar análise"}
</>
)}
</Button>
)}
{isSaved && savedDate && (
<span className="text-sm text-muted-foreground">
Salva em{" "}
{format(new Date(savedDate), "dd/MM/yyyy 'às' HH:mm", {
locale: ptBR,
})}
</span>
)}
</div>
{/* Content Area */}
<div className="min-h-[400px]">
{(isPending || isLoading) && <LoadingState />}
{!isPending && !isLoading && !insights && !error && (
<Card className="flex min-h-[50vh] w-full items-center justify-center py-12">
<EmptyState
media={<RiSparklingLine className="size-6 text-primary" />}
title="Nenhuma análise realizada"
description="Clique no botão acima para gerar insights inteligentes sobre seus
dados financeiros do mês selecionado."
/>
</Card>
)}
{!isPending && !isLoading && error && (
<ErrorState error={error} onRetry={handleAnalyze} />
)}
{!isPending && !isLoading && insights && !error && (
<InsightsGrid insights={insights} />
)}
</div>
</div>
);
}
function LoadingState() {
return (
<div className="space-y-6">
{/* Intro text skeleton */}
<div className="space-y-2 px-1">
<Skeleton className="h-5 w-full max-w-2xl" />
<Skeleton className="h-5 w-full max-w-md" />
</div>
{/* Grid de Cards */}
<div className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
{Array.from({ length: 4 }).map((_, i) => (
<Card key={i} className="relative overflow-hidden">
<CardHeader>
<div className="flex items-center gap-2">
<Skeleton className="size-5 rounded" />
<Skeleton className="h-5 w-32" />
</div>
</CardHeader>
<CardContent>
{Array.from({ length: 4 }).map((_, j) => (
<div
key={j}
className="flex flex-1 border-b border-dashed py-2.5 gap-2 items-start last:border-0"
>
<Skeleton className="size-4 shrink-0 rounded" />
<div className="flex-1 space-y-2">
<Skeleton className="h-4 w-full" />
<Skeleton className="h-4 w-3/4" />
</div>
</div>
))}
</CardContent>
</Card>
))}
</div>
</div>
);
}
function ErrorState({
error,
onRetry,
}: {
error: string;
onRetry: () => void;
}) {
return (
<div className="flex flex-col items-center justify-center gap-4 py-12 px-4 text-center">
<div className="flex flex-col gap-2">
<h3 className="text-lg font-semibold text-destructive">
Erro ao gerar insights
</h3>
<p className="text-sm text-muted-foreground max-w-md">{error}</p>
</div>
<Button onClick={onRetry} variant="outline">
Tentar novamente
</Button>
</div>
);
}

View File

@@ -0,0 +1,236 @@
"use client";
import { RiExternalLinkLine } from "@remixicon/react";
import Image from "next/image";
import { useEffect, useMemo, useState } from "react";
import {
type AIProvider,
AVAILABLE_MODELS,
DEFAULT_PROVIDER,
PROVIDERS,
} from "@/features/insights/constants";
import { Card } from "@/shared/components/ui/card";
import { Input } from "@/shared/components/ui/input";
import { Label } from "@/shared/components/ui/label";
import { RadioGroup, RadioGroupItem } from "@/shared/components/ui/radio-group";
import {
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
} from "@/shared/components/ui/select";
interface ModelSelectorProps {
value: string;
onValueChange: (value: string) => void;
disabled?: boolean;
}
const PROVIDER_ICON_PATHS: Record<
AIProvider,
{ light: string; dark?: string }
> = {
openai: {
light: "/providers/chatgpt.svg",
dark: "/providers/chatgpt_dark_mode.svg",
},
anthropic: {
light: "/providers/claude.svg",
},
google: {
light: "/providers/gemini.svg",
},
openrouter: {
light: "/providers/openrouter_light.svg",
dark: "/providers/openrouter_dark.svg",
},
};
export function ModelSelector({
value,
onValueChange,
disabled,
}: ModelSelectorProps) {
// Estado para armazenar o provider selecionado manualmente
const [selectedProvider, setSelectedProvider] = useState<AIProvider | null>(
null,
);
const [customModel, setCustomModel] = useState(value);
// Sincronizar customModel quando value mudar (importante para pré-carregamento)
useEffect(() => {
// Se o value tem "/" é um modelo OpenRouter customizado
if (value.includes("/")) {
setCustomModel(value);
setSelectedProvider("openrouter");
} else {
setCustomModel(value);
// Limpar selectedProvider para deixar o useMemo detectar automaticamente
setSelectedProvider(null);
}
}, [value]);
// Determinar provider atual baseado no modelo selecionado ou provider manual
const currentProvider = useMemo(() => {
// Se há um provider selecionado manualmente, use-o
if (selectedProvider) {
return selectedProvider;
}
// Se o modelo tem "/" é OpenRouter
if (value.includes("/")) {
return "openrouter";
}
// Caso contrário, tente detectar baseado no modelo
const model = AVAILABLE_MODELS.find((m) => m.id === value);
return model?.provider ?? DEFAULT_PROVIDER;
}, [value, selectedProvider]);
// Agrupar modelos por provider
const modelsByProvider = useMemo(() => {
const grouped: Record<
AIProvider,
Array<(typeof AVAILABLE_MODELS)[number]>
> = {
openai: [],
anthropic: [],
google: [],
openrouter: [],
};
AVAILABLE_MODELS.forEach((model) => {
grouped[model.provider].push(model);
});
return grouped;
}, []);
// Atualizar provider (seleciona primeiro modelo daquele provider)
const handleProviderChange = (newProvider: AIProvider) => {
setSelectedProvider(newProvider);
if (newProvider === "openrouter") {
// Para OpenRouter, usa o modelo customizado ou limpa o valor
onValueChange(customModel || "");
return;
}
const firstModel = modelsByProvider[newProvider][0];
if (firstModel) {
onValueChange(firstModel.id);
}
};
// Atualizar modelo customizado do OpenRouter
const handleCustomModelChange = (modelName: string) => {
setCustomModel(modelName);
onValueChange(modelName);
};
return (
<Card className="grid grid-cols-1 lg:grid-cols-[1fr,auto] gap-6 items-start p-6">
{/* Descrição */}
<div className="space-y-2">
<h3 className="text-lg font-semibold">Definir modelo de análise</h3>
<p className="text-sm text-muted-foreground leading-relaxed">
Escolha o provedor de IA e o modelo específico que será utilizado para
gerar insights sobre seus dados financeiros. <br />
Diferentes modelos podem oferecer perspectivas variadas na análise.
</p>
</div>
{/* Seletor */}
<div className="flex flex-col gap-4 min-w-xs">
<RadioGroup
value={currentProvider}
onValueChange={(v) => handleProviderChange(v as AIProvider)}
disabled={disabled}
className="gap-3"
>
{(Object.keys(PROVIDERS) as AIProvider[]).map((providerId) => {
const provider = PROVIDERS[providerId];
const iconPaths = PROVIDER_ICON_PATHS[providerId];
return (
<div key={providerId} className="flex items-center gap-3">
<RadioGroupItem
value={providerId}
id={`provider-${providerId}`}
disabled={disabled}
/>
<div className="size-6 relative">
<Image
src={iconPaths.light}
alt={provider.name}
width={22}
height={22}
className={iconPaths.dark ? "dark:hidden" : ""}
/>
{iconPaths.dark && (
<Image
src={iconPaths.dark}
alt={provider.name}
width={22}
height={22}
className="hidden dark:block"
/>
)}
</div>
<Label
htmlFor={`provider-${providerId}`}
className="text-sm font-medium cursor-pointer flex-1"
>
{provider.name}
</Label>
</div>
);
})}
</RadioGroup>
{/* Seletor de Modelo */}
{currentProvider === "openrouter" ? (
<div className="space-y-2">
<Input
value={customModel}
onChange={(e) => handleCustomModelChange(e.target.value)}
placeholder="Ex: anthropic/claude-3.5-sonnet"
disabled={disabled}
className="border-none bg-neutral-200 dark:bg-neutral-800"
/>
<a
href="https://openrouter.ai/models"
target="_blank"
rel="noopener noreferrer"
className="flex items-center gap-1.5 text-xs text-muted-foreground hover:text-foreground transition-colors"
>
<RiExternalLinkLine className="h-3 w-3" />
Ver modelos disponíveis no OpenRouter
</a>
</div>
) : (
<Select
value={value}
onValueChange={onValueChange}
disabled={disabled}
>
<SelectTrigger
disabled={disabled}
className="border-none bg-neutral-200 dark:bg-neutral-800"
>
<SelectValue placeholder="Selecione um modelo" />
</SelectTrigger>
<SelectContent>
{modelsByProvider[currentProvider].map((model) => (
<SelectItem key={model.id} value={model.id}>
{model.name}
</SelectItem>
))}
</SelectContent>
</Select>
)}
</div>
</Card>
);
}

View File

@@ -0,0 +1,149 @@
/**
* Tipos de providers disponíveis
*/
export type AIProvider = "openai" | "anthropic" | "google" | "openrouter";
/**
* Metadados dos providers
*/
export const PROVIDERS = {
openai: {
id: "openai" as const,
name: "ChatGPT",
icon: "RiOpenaiLine",
},
anthropic: {
id: "anthropic" as const,
name: "Claude AI",
icon: "RiRobot2Line",
},
google: {
id: "google" as const,
name: "Gemini",
icon: "RiGoogleLine",
},
openrouter: {
id: "openrouter" as const,
name: "OpenRouter",
icon: "RiRouterLine",
},
} as const;
/**
* Lista de modelos de IA disponíveis para análise de insights
*/
export const AVAILABLE_MODELS = [
// OpenAI Models - GPT-5.2 Family (Latest)
{ id: "gpt-5.2", name: "GPT-5.2", provider: "openai" as const },
{
id: "gpt-5.2-instant",
name: "GPT-5.2 Instant",
provider: "openai" as const,
},
{
id: "gpt-5.2-thinking",
name: "GPT-5.2 Thinking",
provider: "openai" as const,
},
// OpenAI Models - GPT-5 Family
{ id: "gpt-5", name: "GPT-5", provider: "openai" as const },
{ id: "gpt-5-instant", name: "GPT-5 Instant", provider: "openai" as const },
// Anthropic Models - Claude 4.5
{
id: "claude-4.5-haiku",
name: "Claude 4.5 Haiku",
provider: "anthropic" as const,
},
{
id: "claude-4.5-sonnet",
name: "Claude 4.5 Sonnet",
provider: "anthropic" as const,
},
{
id: "claude-opus-4.1",
name: "Claude 4.1 Opus",
provider: "anthropic" as const,
},
// Google Models - Gemini 3 (Latest)
{
id: "gemini-3-flash-preview",
name: "Gemini 3 Flash",
provider: "google" as const,
},
{
id: "gemini-3-pro-preview",
name: "Gemini 3 Pro",
provider: "google" as const,
},
// Google Models - Gemini 2.0
{
id: "gemini-2.0-flash",
name: "Gemini 2.0 Flash",
provider: "google" as const,
},
] as const;
export const DEFAULT_MODEL = "gpt-5.2";
export const DEFAULT_PROVIDER = "openai";
/**
* System prompt para análise de insights
*/
export const INSIGHTS_SYSTEM_PROMPT = `Você é um especialista em comportamento financeiro. Analise os dados financeiros fornecidos e organize suas observações em 4 categorias específicas:
1. **Comportamentos Observados** (behaviors): Padrões de gastos e hábitos financeiros identificados nos dados. Foque em comportamentos recorrentes e tendências. Considere:
- Tendência dos últimos 3 meses (crescente, decrescente, estável)
- Gastos recorrentes e sua previsibilidade
- Padrões de parcelamento e comprometimento futuro
2. **Gatilhos de Consumo** (triggers): Identifique situações, períodos ou categorias que desencadeiam maiores gastos. O que leva o usuário a gastar mais? Analise:
- Dias da semana com mais gastos
- Categorias que cresceram nos últimos meses
- Métodos de pagamento que facilitam gastos
3. **Recomendações Práticas** (recommendations): Sugestões concretas e acionáveis para melhorar a saúde financeira. Seja específico e direto. Use os dados de:
- Gastos recorrentes que podem ser otimizados
- Orçamentos que estão sendo ultrapassados
- Comprometimento futuro com parcelamentos
4. **Melhorias Sugeridas** (improvements): Oportunidades de otimização e estratégias de longo prazo para alcançar objetivos financeiros. Considere:
- Tendências preocupantes dos últimos 3 meses
- Percentual de gastos recorrentes vs pontuais
- Estratégias para reduzir comprometimento futuro
Para cada categoria, forneça de 3 a 6 itens concisos e objetivos. Use linguagem clara e direta, com verbos de ação. Mantenha privacidade e não exponha dados pessoais sensíveis.
IMPORTANTE: Utilize os novos dados disponíveis (threeMonthTrend, recurringExpenses, installments) para fornecer insights mais ricos e contextualizados.
Responda EXCLUSIVAMENTE com um JSON válido seguindo o esquema:
{
"month": "YYYY-MM",
"generatedAt": "ISO datetime",
"categories": [
{
"category": "behaviors",
"items": [
{ "text": "Observação aqui" },
...
]
},
{
"category": "triggers",
"items": [...]
},
{
"category": "recommendations",
"items": [...]
},
{
"category": "improvements",
"items": [...]
}
]
}
`;