import {
  Controller,
  Post,
  Get,
  Patch,
  Delete,
  Body,
  Query,
  Param,
  UseInterceptors,
  UploadedFiles,
  UseGuards,
  ParseIntPipe,
  Req,
} from "@nestjs/common"
import {
  ApiBearerAuth,
  ApiBody,
  ApiConsumes,
  ApiOperation,
  ApiParam,
  ApiQuery,
  ApiTags,
} from "@nestjs/swagger"
import { FileFieldsInterceptor } from "@nestjs/platform-express"
import { CreateCustomerTreatmentPlanDto } from "../dto/create-customer-treatment-plan.dto"
import { UpdateCustomerTreatmentPlanDto } from "../dto/update-customer-treatment-plan.dto"
import { CustomerTreatmentPlanService } from "./customer-treatment-plan.service"
import { customerTreatmentPlanUploadConfig } from "src/common/file-upload/customer-treatment-plan-images.config"
import { CustomerAuthGuardMiddleware } from "src/middleware/customer-auth-guard.middleware"
import { AuthGuardMiddleware } from "src/middleware/auth-guard.middleware"
@ApiTags("Customer Treatment Plan")
@Controller("customer-treatment-plan")
export class CustomerTreatmentPlanController {
  constructor(
    private readonly customerTreatmentPlanService: CustomerTreatmentPlanService,
  ) {}

  @Post()
  @UseGuards(CustomerAuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @ApiOperation({
    summary: "Create a customer treatment plan with multiple images",
  })
  @ApiConsumes("multipart/form-data")
  @ApiBody({
    schema: {
      type: "object",
      properties: {
        comments: { type: "string", example: "Initial treatment notes" },
        images: {
          type: "array",
          items: { type: "string", format: "binary" },
        },
      },
    },
  })
  @UseInterceptors(
    FileFieldsInterceptor(
      [{ name: "images", maxCount: 10 }],
      customerTreatmentPlanUploadConfig,
    ),
  )
  async create(
    @Body() dto: CreateCustomerTreatmentPlanDto,
    @UploadedFiles() files: { images?: Express.Multer.File[] },
    @Req() req: any,
  ) {
    dto.customer_id = req.customer.customer_id
    return this.customerTreatmentPlanService.create(dto, files)
  }

  @Get()
  @UseGuards(CustomerAuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @ApiOperation({ summary: "Get all customer treatment plans" })
  @ApiQuery({ name: "limit", required: false, type: Number })
  @ApiQuery({ name: "skip", required: false, type: Number })
  @ApiQuery({ name: "search", required: false, type: String })
  @ApiQuery({ name: "sortBy", required: false, type: String })
  @ApiQuery({ name: "sortOrder", required: false, type: String })
  @ApiQuery({ name: "customer_id", required: false, type: Number })
  async findAll(
    @Query("limit") limit?: number,
    @Query("skip") skip?: number,
    @Query("search") search?: string,
    @Query("sortBy") sortBy?: string,
    @Query("sortOrder") sortOrder?: string,
    @Query("customer_id") customer_id?: number,
  ) {
    return this.customerTreatmentPlanService.findAll(
      limit ? +limit : undefined,
      skip ? +skip : undefined,
      search,
      sortBy,
      sortOrder,
      customer_id,
    )
  }

  @Get("admin")
  @UseGuards(AuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @ApiOperation({ summary: "Get all customer treatment plans" })
  @ApiQuery({ name: "limit", required: false, type: Number })
  @ApiQuery({ name: "skip", required: false, type: Number })
  @ApiQuery({ name: "search", required: false, type: String })
  @ApiQuery({ name: "sortBy", required: false, type: String })
  @ApiQuery({ name: "sortOrder", required: false, type: String })
  @ApiQuery({ name: "customer_id", required: false, type: Number })
  async findAllForAdmin(
    @Query("limit") limit?: number,
    @Query("skip") skip?: number,
    @Query("search") search?: string,
    @Query("sortBy") sortBy?: string,
    @Query("sortOrder") sortOrder?: string,
    @Query("customer_id") customer_id?: number,
  ) {
    return this.customerTreatmentPlanService.findAll(
      limit ? +limit : undefined,
      skip ? +skip : undefined,
      search,
      sortBy,
      sortOrder,
      customer_id,
    )
  }

  @Get(":id")
  @UseGuards(CustomerAuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @ApiOperation({ summary: "Get a specific customer treatment plan by ID" })
  @ApiParam({ name: "id", type: Number })
  async findOne(@Param("id", ParseIntPipe) id: number) {
    return this.customerTreatmentPlanService.findOne(id)
  }

  @Patch(":id")
  @UseGuards(CustomerAuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @ApiOperation({
    summary: "Update a customer treatment plan and optionally add images",
  })
  @ApiConsumes("multipart/form-data")
  @ApiParam({ name: "id", type: Number })
  @ApiBody({
    schema: {
      type: "object",
      properties: {
        comments: { type: "string", example: "Updated treatment notes" },
        images: {
          type: "array",
          items: { type: "string", format: "binary" },
        },
      },
    },
  })
  @UseInterceptors(
    FileFieldsInterceptor(
      [{ name: "images", maxCount: 10 }],
      customerTreatmentPlanUploadConfig,
    ),
  )
  async update(
    @Param("id", ParseIntPipe) id: number,
    @Body() dto: UpdateCustomerTreatmentPlanDto,
    @UploadedFiles() files?: { images?: Express.Multer.File[] },
  ) {
    return this.customerTreatmentPlanService.update(id, dto, files)
  }

  @Delete(":id")
  @UseGuards(CustomerAuthGuardMiddleware)
  @ApiBearerAuth("access-token")
  @ApiOperation({ summary: "Delete a customer treatment plan by ID" })
  @ApiParam({ name: "id", type: Number })
  async remove(@Param("id", ParseIntPipe) id: number) {
    return this.customerTreatmentPlanService.remove(id)
  }
}
