import {
  Controller,
  Get,
  Post,
  Patch,
  Param,
  UseGuards,
  Request,
  ParseUUIDPipe,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiParam,
} from '@nestjs/swagger';
import { PaymentsService } from './payments.service';
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard';

@ApiTags('payments')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('payment-methods')
export class PaymentsController {
  constructor(private readonly paymentsService: PaymentsService) {}

  @Get()
  @ApiOperation({ summary: 'Listar métodos de pago' })
  @ApiResponse({ status: 200, description: 'Lista de métodos de pago' })
  async findAll(@Request() req: { user: { tenantId: string } }) {
    return this.paymentsService.findAll(req.user.tenantId);
  }

  @Get('default')
  @ApiOperation({ summary: 'Obtener método de pago por defecto' })
  async getDefault(@Request() req: { user: { tenantId: string } }) {
    return this.paymentsService.getDefault(req.user.tenantId);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Obtener método de pago por ID' })
  @ApiParam({ name: 'id', description: 'ID del método de pago' })
  async findOne(
    @Request() req: { user: { tenantId: string } },
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    return this.paymentsService.findOne(req.user.tenantId, id);
  }

  @Post('initialize')
  @ApiOperation({ summary: 'Inicializar métodos de pago por defecto' })
  @ApiResponse({ status: 201, description: 'Métodos de pago inicializados' })
  async initialize(@Request() req: { user: { tenantId: string } }) {
    return this.paymentsService.initializeForTenant(req.user.tenantId);
  }

  @Patch(':id/toggle-active')
  @ApiOperation({ summary: 'Activar/desactivar método de pago' })
  @ApiParam({ name: 'id', description: 'ID del método de pago' })
  async toggleActive(
    @Request() req: { user: { tenantId: string } },
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    return this.paymentsService.toggleActive(req.user.tenantId, id);
  }

  @Patch(':id/set-default')
  @ApiOperation({ summary: 'Establecer como método de pago por defecto' })
  @ApiParam({ name: 'id', description: 'ID del método de pago' })
  async setDefault(
    @Request() req: { user: { tenantId: string } },
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    return this.paymentsService.setDefault(req.user.tenantId, id);
  }
}
