import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
  Query,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
  UseGuards,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiBearerAuth,
  ApiParam,
} from '@nestjs/swagger';
import { DealsService } from './deals.service';
import { CreateDealDto } from './dto/create-deal.dto';
import { UpdateDealDto } from './dto/update-deal.dto';
import { QueryDealsDto } from './dto/query-deals.dto';
import { ForecastQueryDto } from './dto/forecast-query.dto';
import { AddOwnerDto } from '../common/dto/owner.dto';
import { OwnersService } from '../owners/owners.service';
import { CurrentUser, JwtPayload } from '../common/decorators';
import { TenantGuard } from '../auth/guards/tenant.guard';
import {
  paginatedResponse,
  singleResponse,
} from '../common/dto/pagination.dto';

@ApiTags('Vorgaenge (Deals)')
@ApiBearerAuth('access-token')
@UseGuards(TenantGuard)
@Controller('deals')
export class DealsController {
  constructor(
    private readonly dealsService: DealsService,
    private readonly ownersService: OwnersService,
  ) {}

  @Post()
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({ summary: 'Vorgang erstellen' })
  async create(
    @CurrentUser() user: JwtPayload,
    @Body() dto: CreateDealDto,
  ) {
    const deal = await this.dealsService.create(
      user.tenantId!,
      user.sub,
      dto,
    );
    return singleResponse(deal);
  }

  @Get()
  @ApiOperation({ summary: 'Vorgaenge auflisten (paginiert, filterbar)' })
  async findAll(
    @CurrentUser() user: JwtPayload,
    @Query() query: QueryDealsDto,
  ) {
    const result = await this.dealsService.findAll(user.tenantId!, query);
    return paginatedResponse(
      result.data,
      result.total,
      result.page,
      result.pageSize,
    );
  }

  @Get('forecast')
  @ApiOperation({ summary: 'Umsatz-Forecast (gewichtete Pipeline)' })
  async forecast(
    @CurrentUser() user: JwtPayload,
    @Query() query: ForecastQueryDto,
  ) {
    const result = await this.dealsService.forecast(user.tenantId!, query);
    return singleResponse(result);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Vorgangsdetails abrufen' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async findOne(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    const deal = await this.dealsService.findOne(user.tenantId!, id);
    return singleResponse(deal);
  }

  @Patch(':id')
  @ApiOperation({ summary: 'Vorgang aktualisieren' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async update(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdateDealDto,
  ) {
    const deal = await this.dealsService.update(
      user.tenantId!,
      id,
      user.sub,
      dto,
    );
    return singleResponse(deal);
  }

  @Delete(':id')
  @ApiOperation({ summary: 'Vorgang loeschen' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async remove(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    const deal = await this.dealsService.remove(user.tenantId!, id);
    return singleResponse(deal);
  }

  // --------------------------------------------------------
  // Owner Endpoints
  // --------------------------------------------------------

  @Post(':id/owners')
  @HttpCode(HttpStatus.CREATED)
  @ApiOperation({ summary: 'Owner zu Vorgang hinzufuegen' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  async addOwner(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: AddOwnerDto,
  ) {
    const owner = await this.ownersService.addDealOwner(
      user.tenantId!,
      id,
      dto,
    );
    return singleResponse(owner);
  }

  @Delete(':id/owners/:userId')
  @ApiOperation({ summary: 'Owner von Vorgang entfernen' })
  @ApiParam({ name: 'id', type: 'string', format: 'uuid' })
  @ApiParam({ name: 'userId', type: 'string', format: 'uuid' })
  async removeOwner(
    @CurrentUser() user: JwtPayload,
    @Param('id', ParseUUIDPipe) id: string,
    @Param('userId', ParseUUIDPipe) userId: string,
  ) {
    const owner = await this.ownersService.removeDealOwner(
      user.tenantId!,
      id,
      userId,
    );
    return singleResponse(owner);
  }
}
