import { Controller, Get, Post, Body, Patch, Param, Delete, UseGuards, Request } from '@nestjs/common';
import { OpportunitiesService } from './opportunities.service';
import { CreateOpportunityDto } from './dto/create-opportunity.dto';
import { UpdateOpportunityDto } from './dto/update-opportunity.dto';
import { AuthGuard } from '@nestjs/passport';

@Controller('opportunities')
@UseGuards(AuthGuard('jwt'))
export class OpportunitiesController {
    constructor(private readonly opportunitiesService: OpportunitiesService) { }

    @Post()
    create(@Request() req, @Body() createOpportunityDto: CreateOpportunityDto) {
        if (!createOpportunityDto.assignedTo) {
            createOpportunityDto.assignedTo = req.user.id;
        }
        return this.opportunitiesService.create(createOpportunityDto);
    }

    @Get()
    findAll(@Request() req) {
        return this.opportunitiesService.findAll(req.user);
    }

    @Get(':id')
    findOne(@Param('id') id: string) {
        return this.opportunitiesService.findOne(id);
    }

    @Patch(':id')
    update(@Param('id') id: string, @Body() updateOpportunityDto: UpdateOpportunityDto) {
        return this.opportunitiesService.update(id, updateOpportunityDto);
    }

    @Delete(':id')
    remove(@Param('id') id: string) {
        return this.opportunitiesService.remove(id);
    }
}
