import { BadRequestException, Controller, Get, Param, Query, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiQuery, ApiTags } from '@nestjs/swagger';
import { AuthGuard } from '@nestjs/passport';
import { RequireModule } from 'src/auth/decorators/require-module.decorator';
import { RequirePermission } from 'src/auth/decorators/require-permission.decorator';
import type { LoginUserInfo } from 'src/auth/dto/login.dto';
import { GetUser } from 'src/auth/get-user.decorator';
import { ModuleGuard } from 'src/auth/guards/module.guard';
import { PermissionGuard } from 'src/auth/guards/permission.guard';
import { LotesService } from './lotes.service';

@ApiTags('Lotes')
@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'), ModuleGuard)
@RequireModule('INVENTARIO')
@Controller({ path: 'lotes', version: '1' })
export class LotesController {
  constructor(private readonly lotesService: LotesService) {}

  @Get('config')
  @UseGuards(PermissionGuard)
  @RequirePermission('INVENTARIO', 'INV_LOT_LOTE_VER')
  @ApiOperation({ summary: 'Estado de habilitación del módulo LOTES para la empresa' })
  @ApiQuery({ name: 'sucursal_id', required: true })
  async status(@GetUser() user: LoginUserInfo, @Query('sucursal_id') sucursalId?: string) {
    if (!sucursalId) {
      throw new BadRequestException('sucursal_id es obligatorio');
    }
    return {
      message: 'Estado de LOTES obtenido',
      data: await this.lotesService.status(user.empresa_id, sucursalId),
    };
  }

  @Get()
  @UseGuards(PermissionGuard)
  @RequirePermission('INVENTARIO', 'INV_LOT_LOTE_VER')
  @ApiOperation({ summary: 'Listar lotes de inventario por empresa' })
  @ApiQuery({ name: 'page', required: false })
  @ApiQuery({ name: 'limit', required: false })
  @ApiQuery({ name: 'producto_id', required: false })
  @ApiQuery({ name: 'deposito_id', required: false })
  @ApiQuery({ name: 'sucursal_id', required: false })
  @ApiQuery({ name: 'estado', required: false, description: 'vigente | vencido | agotado' })
  @ApiQuery({ name: 'q', required: false, description: 'Búsqueda por lote o descripción de producto' })
  @ApiQuery({ name: 'numero_lote', required: false })
  @ApiQuery({ name: 'fecha_fabricacion_desde', required: false })
  @ApiQuery({ name: 'fecha_fabricacion_hasta', required: false })
  @ApiQuery({ name: 'fecha_vencimiento_desde', required: false })
  @ApiQuery({ name: 'fecha_vencimiento_hasta', required: false })
  @ApiQuery({ name: 'stock_desde', required: false })
  @ApiQuery({ name: 'stock_hasta', required: false })
  @ApiQuery({ name: 'costo_desde', required: false })
  @ApiQuery({ name: 'costo_hasta', required: false })
  findAll(
    @GetUser() user: LoginUserInfo,
    @Query('page') page?: string,
    @Query('limit') limit?: string,
    @Query('sucursal_id') sucursal_id?: string,
    @Query('producto_id') producto_id?: string,
    @Query('deposito_id') deposito_id?: string,
    @Query('estado') estado?: string,
    @Query('q') q?: string,
    @Query('numero_lote') numero_lote?: string,
    @Query('fecha_fabricacion_desde') fecha_fabricacion_desde?: string,
    @Query('fecha_fabricacion_hasta') fecha_fabricacion_hasta?: string,
    @Query('fecha_vencimiento_desde') fecha_vencimiento_desde?: string,
    @Query('fecha_vencimiento_hasta') fecha_vencimiento_hasta?: string,
    @Query('stock_desde') stock_desde?: string,
    @Query('stock_hasta') stock_hasta?: string,
    @Query('costo_desde') costo_desde?: string,
    @Query('costo_hasta') costo_hasta?: string,
  ) {
    return this.lotesService.list(user.empresa_id, {
      page,
      limit,
      sucursal_id,
      producto_id,
      deposito_id,
      estado,
      q,
      numero_lote,
      fecha_fabricacion_desde,
      fecha_fabricacion_hasta,
      fecha_vencimiento_desde,
      fecha_vencimiento_hasta,
      stock_desde,
      stock_hasta,
      costo_desde,
      costo_hasta,
    }, user.id);
  }

  @Get('dashboard')
  @UseGuards(PermissionGuard)
  @RequirePermission('INVENTARIO', 'INV_LOT_LOTE_VER')
  @ApiOperation({ summary: 'Dashboard de lotes (vencidos, por vencer y stock)' })
  @ApiQuery({ name: 'sucursal_id', required: false })
  dashboard(@GetUser() user: LoginUserInfo, @Query('sucursal_id') sucursal_id?: string) {
    return this.lotesService.dashboard(user.empresa_id, sucursal_id, user.id);
  }

  @Get('producto/:productoId')
  @UseGuards(PermissionGuard)
  @RequirePermission('INVENTARIO', 'INV_LOT_LOTE_VER')
  @ApiOperation({ summary: 'Lotes por producto' })
  @ApiQuery({ name: 'deposito_id', required: false })
  @ApiQuery({ name: 'sucursal_id', required: false })
  findByProducto(
    @GetUser() user: LoginUserInfo,
    @Param('productoId') productoId: string,
    @Query('deposito_id') deposito_id?: string,
    @Query('sucursal_id') sucursal_id?: string,
  ) {
    return this.lotesService.findByProducto(user.empresa_id, productoId, deposito_id, sucursal_id);
  }
}
