import {
  Body,
  Controller,
  Delete,
  Get,
  HttpCode,
  HttpStatus,
  Param,
  ParseUUIDPipe,
  Patch,
  Post,
  Put,
} from "@nestjs/common";
import { ApiOperation, ApiTags } from "@nestjs/swagger";

import { FreightAdmin } from "../../common/booking-guards";
import { CreateDropdownOptionDto } from "./dto/create-dropdown-option.dto";
import { CreateDropdownSettingDto } from "./dto/create-dropdown-setting.dto";
import { UpdateDropdownOptionDto } from "./dto/update-dropdown-option.dto";
import { UpdateDropdownSettingDto } from "./dto/update-dropdown-setting.dto";
import { DropdownSettingsService } from "./dropdown-settings.service";

@ApiTags("dropdown-settings")
@Controller("dropdown-settings")
export class DropdownSettingsController {
  constructor(private readonly service: DropdownSettingsService) {}

  // Reads stay open: the customer portal fetches these to render dynamic
  // dropdowns (by-code). Only writes are admin-guarded.

  @Get()
  @ApiOperation({ summary: "List all dropdown settings" })
  list() {
    return this.service.list();
  }

  @Get(":id")
  @ApiOperation({ summary: "Get a dropdown setting by ID" })
  getById(@Param("id", ParseUUIDPipe) id: string) {
    return this.service.getById(id);
  }

  @Get("by-code/:code")
  @ApiOperation({ summary: "Get a dropdown setting by its stable code" })
  getByCode(@Param("code") code: string) {
    return this.service.getByCode(code);
  }

  @Post()
  @FreightAdmin()
  @ApiOperation({ summary: "Create a new dropdown setting" })
  create(@Body() dto: CreateDropdownSettingDto) {
    return this.service.create(dto);
  }

  @Patch(":id")
  @FreightAdmin()
  @ApiOperation({ summary: "Update a dropdown setting's metadata" })
  update(
    @Param("id", ParseUUIDPipe) id: string,
    @Body() dto: UpdateDropdownSettingDto,
  ) {
    return this.service.update(id, dto);
  }

  @Delete(":id")
  @FreightAdmin()
  @ApiOperation({ summary: "Soft-delete a dropdown setting" })
  @HttpCode(HttpStatus.NO_CONTENT)
  remove(@Param("id", ParseUUIDPipe) id: string) {
    return this.service.remove(id);
  }

  /* ------------------------- option routes ------------------------- */

  @Put(":id/options")
  @FreightAdmin()
  @ApiOperation({ summary: "Replace the full option list for a setting" })
  replaceOptions(
    @Param("id", ParseUUIDPipe) id: string,
    @Body() options: CreateDropdownOptionDto[],
  ) {
    return this.service.replaceOptions(id, options);
  }

  @Post(":id/options")
  @FreightAdmin()
  @ApiOperation({ summary: "Append a single option to a setting" })
  addOption(
    @Param("id", ParseUUIDPipe) id: string,
    @Body() dto: CreateDropdownOptionDto,
  ) {
    return this.service.addOption(id, dto);
  }

  @Patch("options/:optionId")
  @FreightAdmin()
  @ApiOperation({ summary: "Update a single option" })
  updateOption(
    @Param("optionId", ParseUUIDPipe) optionId: string,
    @Body() dto: UpdateDropdownOptionDto,
  ) {
    return this.service.updateOption(optionId, dto);
  }

  @Delete("options/:optionId")
  @FreightAdmin()
  @ApiOperation({ summary: "Soft-delete a single option" })
  @HttpCode(HttpStatus.NO_CONTENT)
  removeOption(@Param("optionId", ParseUUIDPipe) optionId: string) {
    return this.service.removeOption(optionId);
  }
}
