import {
  Controller,
  Get,
  Post,
  Patch,
  Delete,
  Body,
  Param,
} from '@nestjs/common';
import {
  ApiTags,
  ApiOperation,
  ApiResponse,
  ApiBearerAuth,
} from '@nestjs/swagger';
import { AlchemyService } from './alchemy.service';
import {
  UpdateVialsDto,
  AddFormulaDto,
  DailyPreparationDto,
  QuickAlchemyDto,
  InitializeAlchemyDto,
  CraftAlchemyDto,
} from './dto';
import { CurrentUser } from '../../common/decorators/current-user.decorator';

@ApiTags('Character Alchemy')
@ApiBearerAuth()
@Controller('campaigns/:campaignId/characters/:characterId/alchemy')
export class AlchemyController {
  constructor(private readonly alchemyService: AlchemyService) {}

  @Get()
  @ApiOperation({ summary: 'Get full alchemy data for a character' })
  @ApiResponse({ status: 200, description: 'Alchemy state, formulas, and prepared items' })
  async getAlchemy(
    @Param('characterId') characterId: string,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.getAlchemy(characterId, userId);
  }

  @Post('initialize')
  @ApiOperation({ summary: 'Initialize or update alchemy state for a character' })
  @ApiResponse({ status: 201, description: 'Alchemy state created/updated' })
  async initializeAlchemy(
    @Param('characterId') characterId: string,
    @Body() dto: InitializeAlchemyDto,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.initializeAlchemy(characterId, dto, userId);
  }

  // Vials
  @Patch('vials')
  @ApiOperation({ summary: 'Update versatile vials current count' })
  @ApiResponse({ status: 200, description: 'Vials updated' })
  async updateVials(
    @Param('characterId') characterId: string,
    @Body() dto: UpdateVialsDto,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.updateVials(characterId, dto.current, userId);
  }

  @Post('vials/refill')
  @ApiOperation({ summary: 'Refill versatile vials (10 minutes exploration activity)' })
  @ApiResponse({ status: 200, description: 'Vials refilled to max' })
  async refillVials(
    @Param('characterId') characterId: string,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.refillVials(characterId, userId);
  }

  // Formulas
  @Get('formulas')
  @ApiOperation({ summary: 'Get all formulas in the formula book' })
  @ApiResponse({ status: 200, description: 'List of formulas' })
  async getFormulas(
    @Param('characterId') characterId: string,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.getFormulas(characterId, userId);
  }

  @Get('formulas/available')
  @ApiOperation({
    summary: 'Get all available formula versions up to character level',
    description:
      'Returns all versions of known formulas that the character can create, ' +
      'including upgraded versions up to their current level. ' +
      'E.g., knowing "Alchemist\'s Fire (Lesser)" grants access to "(Moderate)" version at level 3+.',
  })
  @ApiResponse({ status: 200, description: 'List of available equipment with levels' })
  async getAvailableFormulas(
    @Param('characterId') characterId: string,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.getAvailableFormulas(characterId, userId);
  }

  @Post('formulas')
  @ApiOperation({ summary: 'Add a formula to the formula book' })
  @ApiResponse({ status: 201, description: 'Formula added' })
  async addFormula(
    @Param('characterId') characterId: string,
    @Body() dto: AddFormulaDto,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.addFormula(characterId, dto, userId);
  }

  @Delete('formulas/:formulaId')
  @ApiOperation({ summary: 'Remove a formula from the formula book' })
  @ApiResponse({ status: 200, description: 'Formula removed' })
  async removeFormula(
    @Param('characterId') characterId: string,
    @Param('formulaId') formulaId: string,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.removeFormula(characterId, formulaId, userId);
  }

  // Prepared Items
  @Get('prepared')
  @ApiOperation({ summary: 'Get all prepared/infused items' })
  @ApiResponse({ status: 200, description: 'List of prepared items' })
  async getPreparedItems(
    @Param('characterId') characterId: string,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.getPreparedItems(characterId, userId);
  }

  @Post('prepare')
  @ApiOperation({ summary: 'Daily preparation - create infused items using advanced alchemy' })
  @ApiResponse({ status: 201, description: 'Items prepared successfully' })
  async dailyPreparation(
    @Param('characterId') characterId: string,
    @Body() dto: DailyPreparationDto,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.dailyPreparation(characterId, dto, userId);
  }

  @Post('quick')
  @ApiOperation({ summary: 'Quick Alchemy - create an item using 1 versatile vial' })
  @ApiResponse({ status: 201, description: 'Item created via quick alchemy' })
  async quickAlchemy(
    @Param('characterId') characterId: string,
    @Body() dto: QuickAlchemyDto,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.quickAlchemy(characterId, dto, userId);
  }

  @Post('craft')
  @ApiOperation({ summary: 'Craft permanent alchemical item (does not expire on rest)' })
  @ApiResponse({ status: 201, description: 'Permanent item created' })
  async craftAlchemy(
    @Param('characterId') characterId: string,
    @Body() dto: CraftAlchemyDto,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.craftAlchemy(characterId, dto.equipmentId, dto.quantity, userId);
  }

  @Patch('prepared/:itemId/consume')
  @ApiOperation({ summary: 'Consume/use a prepared item (reduces quantity by 1)' })
  @ApiResponse({ status: 200, description: 'Item consumed' })
  async consumePreparedItem(
    @Param('characterId') characterId: string,
    @Param('itemId') itemId: string,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.consumePreparedItem(characterId, itemId, userId);
  }

  @Delete('prepared/:itemId')
  @ApiOperation({ summary: 'Delete a prepared item completely' })
  @ApiResponse({ status: 200, description: 'Item deleted' })
  async deletePreparedItem(
    @Param('characterId') characterId: string,
    @Param('itemId') itemId: string,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.deletePreparedItem(characterId, itemId, userId);
  }

  @Post('formulas/refresh-translations')
  @ApiOperation({
    summary: 'Refresh translations for all formulas',
    description:
      'Pre-translates all versions of all known formulas. ' +
      'Use this once to populate translations for existing characters.',
  })
  @ApiResponse({ status: 200, description: 'Translations refreshed' })
  async refreshFormulaTranslations(
    @Param('characterId') characterId: string,
    @CurrentUser('id') userId: string,
  ) {
    return this.alchemyService.refreshFormulaTranslations(characterId, userId);
  }
}
