import { Body, Controller, Get, Param, Patch, Post, Req, UseGuards } from '@nestjs/common';
import { Roles } from '../common/decorators/roles.decorator';
import { JwtAuthGuard } from '../common/guards/jwt-auth.guard';
import { RolesGuard } from '../common/guards/roles.guard';
import { FinanceService } from './finance.service';
import { CalculateOrderFinanceDto, CreateWithdrawalDto } from './dto/finance.dto';

@Controller('finance')
@UseGuards(JwtAuthGuard, RolesGuard)
export class FinanceController {
  constructor(private readonly financeService: FinanceService) {}

  @Post('calculate-order')
  calculateOrder(@Body() body: CalculateOrderFinanceDto) {
    return this.financeService.calculateOrder(body);
  }

  @Get('dashboard')
  @Roles('ADMIN', 'SUPER_ADMIN', 'RESTAURANT', 'DRIVER')
  getDashboard(@Req() req: { user: { sub: string; role: string } }) {
    return this.financeService.getDashboard(req.user as never);
  }

  @Get('ledger')
  @Roles('ADMIN', 'SUPER_ADMIN', 'RESTAURANT', 'DRIVER')
  getLedger(@Req() req: { user: { sub: string; role: string } }) {
    return this.financeService.getLedger(req.user as never);
  }

  @Get('restaurants-summary')
  @Roles('ADMIN', 'SUPER_ADMIN')
  getRestaurantsSummary() {
    return this.financeService.getRestaurantsSummary();
  }

  @Post('withdrawals')
  createWithdrawal(@Req() req: { user: { sub: string; role: string } }, @Body() body: CreateWithdrawalDto) {
    return this.financeService.requestWithdrawal(req.user as never, body);
  }

  @Patch('withdrawals/:id/approve')
  @Roles('ADMIN', 'SUPER_ADMIN')
  approveWithdrawal(@Param('id') id: string) {
    return this.financeService.approveWithdrawal(id);
  }
}
