import { Injectable } from '@nestjs/common';
import {
  ApprovalState,
  Ticket,
  TicketStateHistory,
  TicketValidationState,
  Tour,
} from '@prisma/client';
import {
  TicketFilterDto,
  UpdateDocumentInfoDto,
} from 'src/modules/feat-business-objects/entities/ticket/tickets.dto';
import { TicketRepository } from './ticket.repository';

@Injectable()
export class TicketService {
  constructor(private readonly repository: TicketRepository) {}

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

  async findAllCreated(): Promise<(Ticket & { tours: Tour[] })[]> {
    return this.repository.findAllCreated();
  }

  async findAllClosed(): Promise<Ticket[]> {
    return this.repository.findAllClosed();
  }

  async findAll(): Promise<(Ticket & { tours: Tour[] })[]> {
    return this.repository.findAll();
  }

  async findByState(
    state: TicketValidationState,
    filter: TicketFilterDto,
    take?: number,
    skip?: number,
  ) {
    return this.repository.findByState(state, filter, take, skip);
  }

  async findStateHistoryByTicketId(
    ticketId: string,
  ): Promise<{ state: TicketValidationState }[]> {
    return this.repository.findStateHistoryByTicketId(ticketId);
  }

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

  async search(query: string): Promise<(Ticket & { tours: Tour[] })[]> {
    return this.repository.search(query);
  }

  async createTicket(tours: Tour[]): Promise<Ticket> {
    return this.repository.createTicket(tours);
  }

  async addToursToTicket(ticketId: string, tours: Tour[]): Promise<Ticket> {
    return this.repository.addToursToTicket(ticketId, tours);
  }

  async setActionablesOnTicket(
    ticketId: string,
    errors: string[],
    notes: string[],
  ): Promise<Ticket> {
    return this.repository.setActionablesOnTicket(ticketId, errors, notes);
  }

  async setStateOfTicket(
    ticketId: string,
    state: TicketValidationState,
  ): Promise<TicketStateHistory> {
    return this.repository.setStateOfTicket(ticketId, state);
  }

  async updateTicket(
    ticketId: string,
    data: {
      state?: TicketValidationState;
      errors?: string[];
      notes?: string[];
      isUrgent?: boolean;
      urgency?: Date;
      approvalState?: ApprovalState;
      documentInfo?: UpdateDocumentInfoDto;
      approvalPath?: string;
    },
  ): Promise<Ticket & { tours: Tour[] }> {
    return this.repository.updateTicket(ticketId, data);
  }
}
