import { Injectable } from '@nestjs/common';
import { Prisma, Tour } from '@prisma/client';
import { Interval } from 'date-fns';
import { AccountingState } from 'src/modules/app-accounting/accounting-state';
import { TourRepository } from './tour.repository';
import { TourAccountingFilterDto } from './tours.filter';

@Injectable()
export class TourService {
  constructor(private readonly repository: TourRepository) {}

  async findInInterval(
    interval: Interval,
    ordinanceTypes?: string[],
  ): Promise<Tour[]> {
    return this.repository.findInInterval(interval, ordinanceTypes);
  }

  async findUpdatedAtBetween(
    firstId: string,
    lastId: string,
  ): Promise<{ id: string; updatedAt: Date }[]> {
    return this.repository.findUpdatedAtBetween(firstId, lastId);
  }

  public findInIntervalWithEmployee(
    interval: Interval,
    employeeId: string,
    ordinanceTypes: string[],
  ): Promise<Tour[]> {
    return this.repository.findInIntervalWithEmployee(
      interval,
      employeeId,
      ordinanceTypes,
    );
  }

  async findById(tourId: string): Promise<Tour> {
    return this.repository.findById(tourId);
  }

  async findByOperationId(operationId: string): Promise<Tour> {
    return this.repository.findByOperationId(operationId);
  }

  async findByTicketId(ticketId: string): Promise<Tour[]> {
    return this.repository.findByTicketId(ticketId);
  }

  async findFirstRecurring(tour: Tour): Promise<Tour | null> {
    return this.repository.findFirstRecurring(tour);
  }

  async findOnSameDayWithOppositeDirection(tour: Tour): Promise<Tour | null> {
    return this.repository.findOnSameDayWithOppositeDirection(tour);
  }

  async findTragehilfeTour(tour: Tour): Promise<Tour | null> {
    return this.repository.findTragehilfeTour(tour)
  }

  async findToursWithTicket(interval: Interval): Promise<Tour[]> {
    return this.repository.findWithTicket(interval);
  }

  async removeTourFromTicket(tourId: string): Promise<Tour> {
    return this.repository.removeTourFromTicket(tourId);
  }

  async createTour(tourInput: Prisma.TourCreateInput): Promise<Tour> {
    return this.repository.createTour(tourInput);
  }


  async updateTour(
    id: string,
    tourInput: Prisma.TourUpdateInput,
  ): Promise<Tour> {
    return this.repository.updateTour(id, tourInput);
  }

  async deleteTours(ids: string[]): Promise<Prisma.BatchPayload[]> {
    return this.repository.deleteTours(ids);
  }

  async findByAccountingState(
    state: AccountingState,
    filter: TourAccountingFilterDto,
    take?: number,
    skip?: number,
  ): Promise<{ tours: Tour[]; total: number }> {
    return this.repository.findByAccountingState(state, filter, take, skip);
  }

  async calculateAccountingStateMeta(state: AccountingState): Promise<{
    count: number;
    revenueSum?: number;
    positiveRevenueDeviation?: number;
    negativeRevenueDeviation?: number;
  }> {
    return this.repository.calculateAccountingStateMeta(state);
  }
}
