import { Controller, Get, Inject, Logger, Param, Query } from '@nestjs/common';
import { CalendarService } from './calendar.service';
import nano from 'nano';
import { UNLIMITED } from '../consts/consts';

@Controller('calendar')
export class CalendarController {
  private logger = new Logger(CalendarController.name);

  constructor(
    @Inject('CALENDAR_SERVICE')
    private calendarService: CalendarService,
  ) {}

  @Get()
  async get(@Param('filter') filter: any): Promise<string> {
    return await this.calendarService.getICalData(filter);
  }

  @Get('/simple')
  async simple(
    @Query('project') project?: string,
    @Query('category') category?: string,
  ): Promise<string> {
    const andSection: any[] = [
      {
        closed_on: {
          $exists: false,
        },
      },
    ];
    if (project) {
      andSection.push({
        'project.name': {
          $in: [project],
        },
      });
    }
    if (category) {
      andSection.push({
        'category.name': {
          $in: [category],
        },
      });
    }
    const query: nano.MangoQuery = {
      selector: {
        $and: andSection,
      },
      limit: UNLIMITED,
    };
    return await this.calendarService.getICalData(query);
  }
}
