import {
  Controller,
  Get,
  Query,
  Post,
  Body,
  Put,
  Param,
  Patch,
  Delete,
} from '@nestjs/common';
import { CountryService } from './country.service';
import { Public } from '../auth/public.decorator';
import {
  ApiBearerAuth,
  ApiTags,
  ApiBody,
  ApiParam,
  ApiQuery,
  ApiOperation,
} from '@nestjs/swagger';

@ApiTags('country')
@ApiBearerAuth('access-token')
@Controller('country')
export class CountryController {
  constructor(private readonly countryService: CountryService) {}

  @Public()
  @Get()
  @ApiOperation({ summary: 'Get All Countries' })
  findAll() {
    return this.countryService.findAll();
  }
  @Public()
  @Get('filtered')
  @ApiOperation({ summary: 'Get Countries Paginated' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'status', required: false, type: Number })
  findAllPaginated(
    @Query('page') page: any = 1,
    @Query('limit') limit: any = 10,
    @Query('status') status?: any,
  ) {
    const p = !isNaN(Number(page)) ? Number(page) : 1;
    const l = !isNaN(Number(limit)) ? Number(limit) : 10;
    const s =
      status !== undefined && !isNaN(Number(status))
        ? Number(status)
        : undefined;

    return this.countryService.findAllPaginated(p, l, s);
  }

  @Public()
  @Get(':id')
  @ApiOperation({ summary: 'Get Country by ID' })
  @ApiParam({ name: 'id', type: String })
  findOne(@Param('id') id: string) {
    if (isNaN(Number(id))) {
      return { success: false, message: 'Invalid ID' };
    }
    return this.countryService.findOne(+id);
  }

  @Public()
  @Post()
  @ApiOperation({ summary: 'Create Country' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        country_name: { type: 'string' },
        country_code: { type: 'string' },
        currency: { type: 'string' },
        status: { type: 'number', example: 1 },
      },
      required: ['country_name', 'country_code'],
    },
  })
  create(@Body() body: any) {
    return this.countryService.create(body);
  }

  @Public()
  @Put(':id')
  @ApiOperation({ summary: 'Update Country' })
  @ApiParam({ name: 'id', type: String })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        country_name: { type: 'string' },
        country_code: { type: 'string' },
        currency: { type: 'string' },
        status: { type: 'number', example: 1 },
      },
    },
  })
  update(@Param('id') id: string, @Body() body: any) {
    return this.countryService.update(+id, body);
  }

  @Public()
  @Patch(':id')
  @ApiOperation({ summary: 'Patch Country' })
  @ApiParam({ name: 'id', type: String })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        country_name: { type: 'string' },
        country_code: { type: 'string' },
        currency: { type: 'string' },
        status: { type: 'number', example: 1 },
      },
    },
  })
  patch(@Param('id') id: string, @Body() body: any) {
    return this.countryService.update(+id, body);
  }

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