import { Body, Controller, Get, Param, Post } from '@nestjs/common';
import { RedmineTypes } from '../models/redmine-types';
import { IssuesService } from './issues.service';

@Controller('issues')
export class IssuesController {
  constructor(private issuesService: IssuesService) {}

  @Post('find')
  async find(@Body() params: any): Promise<RedmineTypes.Issue[]> {
    return await this.issuesService.find(params);
  }

  @Get('find-from-root/:id')
  async getIssuesFromRoot(
    @Param('id') id: number,
  ): Promise<RedmineTypes.Issue[]> {
    const rootIssue = await this.issuesService.getIssue(id);
    const res = await this.issuesService.getIssuesFromRoot(rootIssue);
    return res;
  }

  @Post('find-from-merged-trees-and-query')
  async findFromMergedTreesAndQuery(
    @Body() params: any,
  ): Promise<RedmineTypes.Issue[]> {
    return await this.issuesService.mergedTreesAndFind(params);
  }

  @Get(':id')
  async getIssue(@Param('id') id: number): Promise<RedmineTypes.Issue> {
    return await this.issuesService.getIssue(id);
  }

  @Get(':id/json')
  async getIssueLikeRedmine(
    @Param('id') id: number,
  ): Promise<{ issue: RedmineTypes.Issue }> {
    return {
      issue: await this.issuesService.getIssue(id),
    };
  }
}
