import { formatMoney } from '@/common/helpers';
import { Expense } from '@/entities/expense.entity';
import { Group } from '@/entities/group.entity';
import {
  NotificationType,
  NotificationPriority,
} from '@/entities/notification.entity';
import { Timeline } from '@/entities/timeline.entity';
import { Trip } from '@/entities/trip.entity';
import { User } from '@/entities/user.entity';

export class NotificationHelper {
  static inviteToGroup(params: {
    group: Group;
    user: User;
    createdBy: string;
  }) {
    const { group, user, createdBy } = params;

    return {
      title: 'Bạn được thêm vào nhóm',
      content: `Bạn vừa được thêm vào nhóm ${group.name}`,
      user,
      createdBy: { id: createdBy } as any,
      group,
      groupKey: group.id,
      type: NotificationType.INVITE,
      priority: NotificationPriority.NORMAL,
      metadata: {
        groupId: group.id,
      },
    };
  }

  static newExpense(params: {
    trip: Trip;
    expense: Expense;
    group: Group;
    user: User;
    createdBy: string;
  }) {
    const { trip, expense, group, user, createdBy } = params;

    return {
      title: 'Có chi phí mới cần duyệt',
      content: `${expense.title} - ${formatMoney(expense.amount)} cho chuyến đi ${trip.name}`,
      user,
      createdBy: { id: createdBy } as any,
      group,
      groupKey: group.id,
      type: NotificationType.EXPENSE,
      metadata: {
        expenseId: expense.id,
        tripId: expense.trip.id,
      },
    };
  }

  static rejectExpense(params: {
    trip: Trip;
    expense: Expense;
    group: Group;
    user: User;
    createdBy: string;
  }) {
    const { trip, expense, group, user, createdBy } = params;

    return {
      title: 'Chi phí bị từ chối',
      content: `${expense.title} - ${formatMoney(expense.amount)} cho chuyến đi ${trip.name} bị từ chối với lý do: ${expense.rejectionReason}`,
      user,
      createdBy: { id: createdBy } as any,
      group,
      groupKey: group.id,
      type: NotificationType.EXPENSE,
      metadata: {
        expenseId: expense.id,
        tripId: expense.trip.id,
      },
    };
  }

  static approveExpense(params: {
    trip: Trip;
    expense: Expense;
    group: Group;
    user: User;
    createdBy: string;
  }) {
    const { trip, expense, group, user, createdBy } = params;

    return {
      title: 'Chi phí được duyệt',
      content: `${expense.title} - ${formatMoney(expense.amount)} cho chuyến đi ${trip.name}`,
      user,
      createdBy: { id: createdBy } as any,
      group,
      groupKey: group.id,
      type: NotificationType.EXPENSE,
      metadata: {
        expenseId: expense.id,
        tripId: expense.trip.id,
      },
    };
  }

  static newTimeline(params: {
    timeline: Timeline;
    trip: Trip;
    group: Group;
    user: User;
    createdBy: string;
  }) {
    const { timeline, trip, group, user, createdBy } = params;

    return {
      title: 'Lịch trình mới',
      content: `Lịch trình ${timeline.title} đã được thêm vào chuyến đi ${trip.name}`,
      user,
      createdBy: { id: createdBy } as any,
      group,
      groupKey: group.id,
      type: NotificationType.TIMELINE,
      metadata: {
        groupId: group.id,
        tripId: trip.id,
      },
    };
  }

  static newTrip(params: {
    trip: Trip;
    group: Group;
    user: User;
    createdBy: User;
  }) {
    const { trip, group, user, createdBy } = params;

    return {
      title: 'Chuyến đi mới',
      content: trip.name,
      user,
      createdBy: createdBy,
      group,
      groupKey: group.id,
      type: NotificationType.TRIP,
      priority: NotificationPriority.NORMAL,
      metadata: {
        groupId: group.id,
        tripId: trip.id,
      },
    };
  }

  static closeTrip(params: {
    trip: Trip;
    group: Group;
    user: User;
    createdBy: string;
  }) {
    const { trip, group, user, createdBy } = params;

    return {
      title: 'Chuyến đi kết thúc',
      content: `Chuyến đi ${trip.name} đã kết thúc, bạn có thể vào xem thanh toán`,
      user,
      createdBy: { id: createdBy } as any,
      group,
      groupKey: group.id,
      type: NotificationType.BALANCE,
      priority: NotificationPriority.HIGH,
      metadata: {
        groupId: group.id,
        tripId: trip.id,
      },
    };
  }

  static notificationTimeline(params: {
    timeline: Timeline;
    group: Group;
    user: User;
    createdBy: string;
  }) {
    const { timeline, group, user, createdBy } = params;

    return {
      title: 'Lịch trình bắt đầu',
      content: `Lịch trình '${timeline.title}' của chuyến đi ${timeline.trip.name} bắt đầu diễn ra, vui lòng chú ý`,
      user,
      createdBy: { id: createdBy } as any,
      group,
      groupKey: group.id,
      type: NotificationType.MANUAL,
      priority: NotificationPriority.HIGH,
      metadata: null,
    };
  }

  static leaderAdd(params: {
    groupId: string;
    user: string;
    createdBy: string;
    title: string;
    content: string;
  }) {
    const { title, content, groupId, user, createdBy } = params;

    return {
      title: title,
      content: content,
      user: { id: user },
      createdBy: { id: createdBy } as any,
      group: { id: groupId },
      groupKey: groupId,
      type: NotificationType.MANUAL,
      priority: NotificationPriority.HIGH,
      metadata: null,
    };
  }
}
