import { Controller, Get, Post, Put, Body, Param, Query, UseGuards, Req } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiBearerAuth } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { IssuerService } from '../../../application/services/issuer.service';
import { RegisterIssuerDto } from '../dto/issuer.dto';

@ApiTags('Issuers')
@Controller('issuers')
export class IssuerController {
  constructor(private readonly issuerService: IssuerService) {}

  @Post()
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Register as issuer' })
  async register(@Req() req: any, @Body() dto: RegisterIssuerDto) {
    const issuer = await this.issuerService.register(req.user.id, dto);
    return { code: 0, data: issuer };
  }

  @Get('me')
  @UseGuards(AuthGuard('jwt'))
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get my issuer profile' })
  async getMyProfile(@Req() req: any) {
    const issuer = await this.issuerService.findByUserId(req.user.id);
    return { code: 0, data: issuer };
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get issuer by ID' })
  async getById(@Param('id') id: string) {
    const issuer = await this.issuerService.findById(id);
    return { code: 0, data: issuer };
  }

  @Get()
  @ApiOperation({ summary: 'List issuers' })
  async list(@Query('page') page: string = '1', @Query('limit') limit: string = '20', @Query('status') status?: string) {
    const result = await this.issuerService.listAll(parseInt(page), parseInt(limit), status);
    return { code: 0, data: result };
  }
}
