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 { AssetService } from '../services/asset.service';
import { CreateAssetDto, UpdateAssetDto } from '../dto';
import { AssetType, AssetStatus } from '../entities/asset.entity';
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('Assets')
@Controller('assets')
@UseGuards(JwtAuthGuard, TenantMemberGuard)
@ApiBearerAuth('JWT-auth')
export class AssetController {
  constructor(private readonly assetService: AssetService) {}

  @Get()
  @ApiOperation({ summary: 'Get all assets with pagination' })
  @ApiQuery({ name: 'brandId', required: false })
  @ApiQuery({ name: 'type', enum: AssetType, required: false })
  @ApiQuery({ name: 'search', required: false })
  @ApiResponse({ status: 200, description: 'List of assets' })
  async findAll(
    @CurrentTenant() tenantId: string,
    @Query() pagination: PaginationDto,
    @Query('brandId') brandId?: string,
    @Query('type') type?: AssetType,
    @Query('search') search?: string,
  ) {
    return this.assetService.findAllPaginated(tenantId, {
      ...pagination,
      brandId,
      type,
      search,
    });
  }

  @Get(':id')
  @ApiOperation({ summary: 'Get asset by ID' })
  @ApiResponse({ status: 200, description: 'Asset found' })
  @ApiResponse({ status: 404, description: 'Asset not found' })
  async findOne(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    return this.assetService.findById(tenantId, id);
  }

  @Get('brand/:brandId')
  @ApiOperation({ summary: 'Get assets by brand' })
  @ApiResponse({ status: 200, description: 'List of brand assets' })
  async findByBrand(
    @CurrentTenant() tenantId: string,
    @Param('brandId', ParseUUIDPipe) brandId: string,
  ) {
    return this.assetService.findByBrand(tenantId, brandId);
  }

  @Post()
  @ApiOperation({ summary: 'Create new asset' })
  @ApiResponse({ status: 201, description: 'Asset created' })
  @ApiResponse({ status: 400, description: 'Bad request' })
  async create(
    @CurrentTenant() tenantId: string,
    @Body() dto: CreateAssetDto,
  ) {
    return this.assetService.create(tenantId, dto);
  }

  @Put(':id')
  @ApiOperation({ summary: 'Update asset' })
  @ApiResponse({ status: 200, description: 'Asset updated' })
  @ApiResponse({ status: 404, description: 'Asset not found' })
  async update(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
    @Body() dto: UpdateAssetDto,
  ) {
    return this.assetService.update(tenantId, id, dto);
  }

  @Put(':id/status')
  @ApiOperation({ summary: 'Update asset status' })
  @ApiResponse({ status: 200, description: 'Asset status updated' })
  @ApiResponse({ status: 404, description: 'Asset not found' })
  async updateStatus(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
    @Body('status') status: AssetStatus,
  ) {
    return this.assetService.updateStatus(tenantId, id, status);
  }

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Delete asset (soft delete)' })
  @ApiResponse({ status: 204, description: 'Asset deleted' })
  @ApiResponse({ status: 404, description: 'Asset not found' })
  async delete(
    @CurrentTenant() tenantId: string,
    @Param('id', ParseUUIDPipe) id: string,
  ) {
    await this.assetService.softDelete(tenantId, id);
  }

  @Delete('bulk')
  @HttpCode(HttpStatus.NO_CONTENT)
  @ApiOperation({ summary: 'Delete multiple assets' })
  @ApiResponse({ status: 204, description: 'Assets deleted' })
  async bulkDelete(
    @CurrentTenant() tenantId: string,
    @Body('ids') ids: string[],
  ) {
    await this.assetService.bulkDelete(tenantId, ids);
  }
}
