import { Injectable, Inject, forwardRef } from '@nestjs/common';
import { ReservationItemDto } from './reservation-item.dto';
import { CatalogService } from '../catalog/catalog.service';
import { CatalogItemLongDto } from '../catalog/dto/catalog-item.dto';

export interface Reservation extends ReservationItemDto {
  id: number;
}

export interface ReservationWithYacht extends Reservation {
  yacht?: CatalogItemLongDto;
}

@Injectable()
export class ReservationsService {
  constructor(
    @Inject(forwardRef(() => CatalogService))
    private readonly catalogService: CatalogService,
  ) {}

  private reservations: Reservation[] = [
    {
      id: 1,
      yachtId: 1,
      reservatorId: 1,
      // Corrected: Jan 1, 2026 20:00 UTC to Jan 2, 2026 08:00 UTC
      startUtc: 1767369600, // Jan 1, 2026 20:00:00 UTC
      endUtc: 1767412800, // Jan 2, 2026 08:00:00 UTC
    },
    {
      id: 2,
      yachtId: 3,
      reservatorId: 2,
      // Jan 3, 2026 08:00 UTC to Jan 5, 2026 20:00 UTC
      startUtc: 1767484800, // Jan 3, 2026 08:00:00 UTC
      endUtc: 1767715200, // Jan 5, 2026 20:00:00 UTC
    },
    {
      id: 3,
      yachtId: 5,
      reservatorId: 1,
      // Jan 10, 2026 20:00 UTC to Jan 12, 2026 08:00 UTC
      startUtc: 1768070400, // Jan 10, 2026 20:00:00 UTC
      endUtc: 1768176000, // Jan 12, 2026 08:00:00 UTC
    },
    {
      id: 4,
      yachtId: 7,
      reservatorId: 2,
      // Jan 15, 2026 08:00 UTC to Jan 17, 2026 20:00 UTC
      startUtc: 1768435200, // Jan 15, 2026 08:00:00 UTC
      endUtc: 1768684800, // Jan 17, 2026 20:00:00 UTC
    },
    {
      id: 5,
      yachtId: 9,
      reservatorId: 1,
      // Jan 20, 2026 20:00 UTC to Jan 22, 2026 08:00 UTC
      startUtc: 1768944000, // Jan 20, 2026 20:00:00 UTC
      endUtc: 1769049600, // Jan 22, 2026 08:00:00 UTC
    },
    {
      id: 6,
      yachtId: 11,
      reservatorId: 2,
      // Jan 25, 2026 08:00 UTC to Jan 27, 2026 20:00 UTC
      startUtc: 1769385600, // Jan 25, 2026 08:00:00 UTC
      endUtc: 1769635200, // Jan 27, 2026 20:00:00 UTC
    },
  ];

  private idCounter = 7;

  createReservation(dto: ReservationItemDto): Reservation {
    const reservation = {
      id: this.idCounter++,
      ...dto,
    };

    this.reservations.push(reservation);
    return reservation;
  }

  async getReservationsByUserId(userId: number): Promise<ReservationWithYacht[]> {
    const reservations = this.reservations.filter((r) => r.reservatorId === userId);
    
    // Populate данные по яхте для каждой резервации
    const reservationsWithYacht = await Promise.all(
      reservations.map(async (reservation) => {
        const yacht = await this.catalogService.getCatalogItemById(reservation.yachtId);
        return {
          ...reservation,
          yacht: yacht || undefined,
        };
      })
    );

    return reservationsWithYacht;
  }

  getReservationsByYachtId(yachtId: number): Reservation[] {
    return this.reservations.filter((r) => r.yachtId === yachtId);
  }

  getAllReservations(): Reservation[] {
    return this.reservations;
  }
}
