import {
  Body,
  Controller,
  Delete,
  Param,
  Patch,
  Post,
  Put,
  UseGuards,
} from '@nestjs/common';
import { SeasonPeriodDataOrchestrator } from '../domain/usecases/season-period-data.orchestrator';
import { SeasonPeriodDto } from './dto/season-period.dto';
import { MODULE_NAME } from 'src/core/strings/constants/module.constants';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { SeasonPeriodEntity } from '../domain/entities/season-period.entity';
import { BatchResult } from 'src/core/response/domain/ok-response.interface';
import { BatchIdsDto } from 'src/core/modules/infrastructure/dto/base-batch.dto';
import { Public } from 'src/core/guards';
import { UpdateSeasonPeriodDto } from './dto/update-season-period.dto';
import { UpdateSeasonPeriodItemDto } from './dto/update-season-period-item.dto';
import { UpdateSeasonPriceDto } from './dto/update-season-price.dto';
import { OtpCheckerGuard } from 'src/core/guards/domain/otp-checker.guard';

@ApiTags(`${MODULE_NAME.SEASON_PERIOD.split('-').join(' ')} - data`)
@Controller(`v1/${MODULE_NAME.SEASON_PERIOD}`)
@Public(false)
@ApiBearerAuth('JWT')
export class SeasonPeriodDataController {
  constructor(private orchestrator: SeasonPeriodDataOrchestrator) {}

  @Post()
  @UseGuards(OtpCheckerGuard)
  async create(@Body() data: SeasonPeriodDto): Promise<SeasonPeriodEntity> {
    return await this.orchestrator.create(data);
  }

  @Post('/update-price')
  @UseGuards(OtpCheckerGuard)
  async updatePrice(@Body() body: UpdateSeasonPriceDto): Promise<BatchResult> {
    return await this.orchestrator.updatePrice(body);
  }

  @Put('/batch-delete')
  async batchDeleted(@Body() body: BatchIdsDto): Promise<BatchResult> {
    return await this.orchestrator.batchDelete(body.ids);
  }

  @Patch(':id/active')
  async active(@Param('id') dataId: string): Promise<string> {
    return await this.orchestrator.active(dataId);
  }

  @Put('/batch-active')
  async batchActive(@Body() body: BatchIdsDto): Promise<BatchResult> {
    return await this.orchestrator.batchActive(body.ids);
  }

  @Patch(':id/confirm')
  async confirm(@Param('id') dataId: string): Promise<string> {
    return await this.orchestrator.confirm(dataId);
  }

  @Put('/batch-confirm')
  async batchConfirm(@Body() body: BatchIdsDto): Promise<BatchResult> {
    return await this.orchestrator.batchConfirm(body.ids);
  }

  @Patch(':id/inactive')
  async inactive(@Param('id') dataId: string): Promise<string> {
    return await this.orchestrator.inactive(dataId);
  }

  @Put('/batch-inactive')
  async batchInactive(@Body() body: BatchIdsDto): Promise<BatchResult> {
    return await this.orchestrator.batchInactive(body.ids);
  }

  @Put(':id')
  async update(
    @Param('id') dataId: string,
    @Body() data: UpdateSeasonPeriodDto,
  ): Promise<SeasonPeriodEntity> {
    return await this.orchestrator.update(dataId, data);
  }

  // pemisahan update data dengan update items dikarenakan payload (based on tampilan) berbeda
  // TODO => simpan OTP update yang disikim dari request ini
  @Put(':id/items')
  @UseGuards(OtpCheckerGuard)
  async updateItems(
    @Param('id') dataId: string,
    @Body() data: UpdateSeasonPeriodItemDto,
  ): Promise<SeasonPeriodEntity> {
    return await this.orchestrator.update(dataId, data, true);
  }

  @Delete(':id')
  async delete(@Param('id') dataId: string): Promise<string> {
    return await this.orchestrator.delete(dataId);
  }
}
