import { Body, Controller, Get, Param, Patch, Post, Query } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { FiscalYearsService } from '../services/fiscal-years.service';
import { CreateFiscalYearDto } from '../dto/create-fiscal-year.dto';
import { Roles } from '../../../common/decorators/roles.decorator';

@ApiTags('Finance — Fiscal Years')
@ApiBearerAuth()
@Controller('fiscal-years')
export class FiscalYearsController {
  constructor(private readonly fiscalYears: FiscalYearsService) {}

  @Roles('SYNDIC')
  @Post()
  create(@Body() dto: CreateFiscalYearDto) {
    return this.fiscalYears.create({
      complexId: dto.complexId,
      label: dto.label,
      startDate: new Date(dto.startDate),
      endDate: new Date(dto.endDate),
    });
  }

  @Get()
  list(@Query('complexId') complexId: string) {
    return this.fiscalYears.findByComplex(complexId);
  }

  @Roles('SYNDIC')
  @Patch(':id/close')
  close(@Param('id') id: string) {
    return this.fiscalYears.close(id);
  }
}
