import {
  Controller,
  Get,
  Post,
  Put,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  ParseUUIDPipe,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
  ApiQuery,
} from '@nestjs/swagger';
import { BrandService } from '../services/brand.service';
import { CreateBrandDto } from '../dto/create-brand.dto';
import { UpdateBrandDto } from '../dto/update-brand.dto';
import { JwtAuthGuard } from '../../../common/guards/jwt-auth.guard';
import { TenantMemberGuard } from '../../../common/guards/tenant-member.guard';
import { CurrentTenant } from '../../../common/decorators/current-tenant.decorator';
import { PaginationDto } from '../../../shared/dto/pagination.dto';

@ApiTags('CRM - Brands')
@Controller('crm/brands')
@UseGuards(JwtAuthGuard, TenantMemberGuard)
@ApiBearerAuth('JWT-auth')
export class BrandController {
  constructor(private readonly brandService: BrandService) {}

  @Get()
  @ApiOperation({ summary: 'Listar marcas del tenant' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiQuery({ name: 'search', required: false, type: String })
  @ApiQuery({ name: 'clientId', required: false, type: String })
  @ApiQuery({ name: 'sortBy', required: false, type: String })
  @ApiQuery({ name: 'sortOrder', required: false, enum: ['ASC', 'DESC'] })
  async findAll(
    @CurrentTenant() tenantId: string,
    @Query() pagination: PaginationDto,
    @Query('clientId') clientId?: string,
  ) {
    return this.brandService.findAllPaginated(tenantId, pagination, clientId);
  }

  @Get(':id')
  @ApiOperation({ summary: 'Obtener marca por ID' })
  @ApiResponse({ status: 200, description: 'Marca encontrada' })
  @ApiResponse({ status: 404, description: 'Marca no encontrada' })
  async findOne(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    return this.brandService.findOneWithClient(tenantId, id);
  }

  @Get('client/:clientId')
  @ApiOperation({ summary: 'Listar marcas de un cliente' })
  async findByClient(
    @CurrentTenant() tenantId: string,
    @Param('clientId', ParseUUIDPipe) clientId: string,
  ) {
    return this.brandService.findByClientId(tenantId, clientId);
  }

  @Post()
  @ApiOperation({ summary: 'Crear nueva marca' })
  @ApiResponse({ status: 201, description: 'Marca creada' })
  @ApiResponse({ status: 409, description: 'Slug ya existe' })
  @ApiResponse({ status: 404, description: 'Cliente no encontrado' })
  async create(
    @CurrentTenant() tenantId: string,
    @Body() dto: CreateBrandDto,
  ) {
    return this.brandService.create(tenantId, dto);
  }

  @Put(':id')
  @ApiOperation({ summary: 'Actualizar marca' })
  @ApiResponse({ status: 200, description: 'Marca actualizada' })
  @ApiResponse({ status: 404, description: 'Marca no encontrada' })
  async update(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdateBrandDto,
  ) {
    return this.brandService.update(tenantId, id, dto);
  }

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Eliminar marca (soft delete)' })
  @ApiResponse({ status: 204, description: 'Marca eliminada' })
  @ApiResponse({ status: 404, description: 'Marca no encontrada' })
  async remove(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    await this.brandService.remove(tenantId, id);
  }
}
