import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Put,
  UseGuards,
} from '@nestjs/common';
import { DataSchedulingDataOrchestrator } from '../domain/usecases/data-scheduling-data.orchestrator';
import {
  CreateDataSchedulingDto,
  EditDataSchedulingDto,
  EditDataSchedulingDefaultDto,
} from './dto/data-scheduling.dto';
import { MODULE_NAME } from 'src/core/strings/constants/module.constants';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import {
  DataSchedulingDefaultEntity,
  DataSchedulingEntity,
} from '../domain/entities/data-scheduling.entity';
import { BatchResult } from 'src/core/response/domain/ok-response.interface';
import { BatchIdsDto } from 'src/core/modules/infrastructure/dto/base-batch.dto';
import { ExcludePrivilege, Public } from 'src/core/guards';
import { SetupSchedulingGuard } from './guards/setup-scheduling.guard';
import { DataSchedulingManager } from '../domain/usecases/managers/data-scheduling-default.manager';

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

  @Post()
  async create(
    @Body() data: CreateDataSchedulingDto,
  ): Promise<DataSchedulingEntity> {
    return await this.orchestrator.create(data);
  }

  @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: EditDataSchedulingDto,
  ): Promise<DataSchedulingEntity> {
    return await this.orchestrator.update(dataId, data);
  }

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

@ApiTags(
  `${MODULE_NAME.DATA_SCHEDULING_DEFAULT.split('-').join(' ')} default - Data`,
)
@Controller(`v1/${MODULE_NAME.DATA_SCHEDULING_DEFAULT}`)
@Public(false)
@ApiBearerAuth('JWT')
export class DataSchedulingDefaultController {
  constructor(private manager: DataSchedulingManager) {}
  @Post()
  async create(
    @Body() data: EditDataSchedulingDefaultDto,
  ): Promise<DataSchedulingDefaultEntity> {
    return await this.manager.update(data);
  }

  @Get()
  async get(): Promise<DataSchedulingDefaultEntity> {
    return await this.manager.getData();
  }
}

@ApiTags(
  `${MODULE_NAME.DATA_SCHEDULING_SETUP.split('-').join(' ')} setup - Data`,
)
@Controller(`v1/${MODULE_NAME.DATA_SCHEDULING_SETUP}`)
@Public(true)
@ApiBearerAuth('JWT')
export class DataSchedulingSetupController {
  constructor(private manager: DataSchedulingManager) {}

  @Post()
  @ExcludePrivilege()
  @UseGuards(SetupSchedulingGuard)
  async setup(): Promise<{ message: string }> {
    return this.manager.setupActiveScheduling();
  }
}
