import { Controller, Get, Put, Body, UseGuards } from '@nestjs/common';
import {
  ApiTags,
  ApiBearerAuth,
  ApiOperation,
  ApiResponse,
} from '@nestjs/swagger';
import { JwtAuthGuard } from '../../common/guards/jwt-auth.guard';
import { CurrentTenant } from '../../common/decorators/current-tenant.decorator';
import { RequirePermission } from '../../common/decorators/require-permission.decorator';
import { SettingsService } from './settings.service';
import { UpdatePublicSiteDto } from './dto/update-public-site.dto';
import { UpdateDomainDto } from './dto/update-domain.dto';

@ApiTags('settings')
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@Controller('settings')
export class SettingsController {
  constructor(private readonly settingsService: SettingsService) {}

  @Get('public-site')
  @RequirePermission('settings.view')
  @ApiOperation({
    summary: 'Get public site configuration',
    description: 'Returns public site settings for this license',
  })
  @ApiResponse({
    status: 200,
    description: 'Public site config retrieved successfully',
  })
  async getPublicSite(@CurrentTenant() licenseId: string) {
    return await this.settingsService.getPublicSite(licenseId);
  }

  @Put('public-site')
  @RequirePermission('settings.manage')
  @ApiOperation({
    summary: 'Update public site configuration',
    description: 'Update public site settings for this license',
  })
  @ApiResponse({
    status: 200,
    description: 'Public site config updated successfully',
  })
  async updatePublicSite(
    @CurrentTenant() licenseId: string,
    @Body() dto: UpdatePublicSiteDto,
  ) {
    return await this.settingsService.updatePublicSite(licenseId, dto);
  }

  @Put('domain')
  @RequirePermission('settings.manage')
  @ApiOperation({
    summary: 'Update domain settings',
    description: 'Update subdomain or custom domain for this license',
  })
  @ApiResponse({
    status: 200,
    description: 'Domain settings updated successfully',
  })
  @ApiResponse({
    status: 400,
    description: 'Invalid domain format',
  })
  @ApiResponse({
    status: 409,
    description: 'Subdomain already taken',
  })
  async updateDomain(
    @CurrentTenant() licenseId: string,
    @Body() dto: UpdateDomainDto,
  ) {
    return await this.settingsService.updateDomain(licenseId, dto);
  }
}
