import {
  Controller,
  Get,
  Post,
  Delete,
  Body,
  Param,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { ConfigSettingService } from './config_setting.service';
import { Public } from '../auth/public.decorator';
import {
  ApiBearerAuth,
  ApiTags,
  ApiBody,
  ApiParam,
  ApiQuery,
  ApiOperation,
} from '@nestjs/swagger';

@ApiTags('config_settings')
@ApiBearerAuth('access-token')
@Controller('config_settings')
export class ConfigSettingController {
  constructor(private readonly configSettingService: ConfigSettingService) {}

  @Public()
  @Get()
  @ApiOperation({ summary: 'Get All Config Settings' })
  async getAllSettings() {
    try {
      const result = await this.configSettingService.getAllSettings();
      if (!result.success) {
        throw new HttpException(result.error, HttpStatus.BAD_REQUEST);
      }
      return result.data;
    } catch (error: any) {
      throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  @Public()
  @Get(':key')
  @ApiOperation({ summary: 'Get Config Setting by Key' })
  @ApiParam({ name: 'key', type: String })
  async getSettingByKey(@Param('key') key: string) {
    try {
      const result = await this.configSettingService.getSettingByKey(key);
      if (!result.success) {
        throw new HttpException(result.error, HttpStatus.BAD_REQUEST);
      }
      return result.data;
    } catch (error: any) {
      throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  @Public()
  @Post()
  @ApiOperation({ summary: 'Create or Update Config Setting' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        setting_key: { type: 'string' },
        setting_value: { type: 'string' },
        description: { type: 'string' },
      },
      required: ['setting_key', 'setting_value'],
    },
  })
  async createOrUpdateSetting(
    @Body()
    body: {
      setting_key: string;
      setting_value: string;
      description?: string;
    },
  ) {
    try {
      const result = await this.configSettingService.createOrUpdateSetting(
        body.setting_key,
        body.setting_value,
        body.description,
      );
      if (!result.success) {
        throw new HttpException(result.error, HttpStatus.BAD_REQUEST);
      }
      return result;
    } catch (error: any) {
      throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  @Public()
  @Delete(':key')
  @ApiOperation({ summary: 'Delete Config Setting' })
  @ApiParam({ name: 'key', type: String })
  async deleteSetting(@Param('key') key: string) {
    try {
      const result = await this.configSettingService.deleteSetting(key);
      if (!result.success) {
        throw new HttpException(result.error, HttpStatus.BAD_REQUEST);
      }
      return result;
    } catch (error: any) {
      throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }

  @Public()
  @Get('cv-limit/free')
  @ApiOperation({ summary: 'Get Free CV Limit' })
  async getCvLimitFree() {
    try {
      const limit = await this.configSettingService.getCvLimitFree();
      return { cv_limit_free: limit };
    } catch (error: any) {
      throw new HttpException(error.message, HttpStatus.INTERNAL_SERVER_ERROR);
    }
  }
}
