import { Controller, Request, Post, Body, Get, Res, UseGuards, Param, Put, Query, Delete, Patch, Req, HttpException } from '@nestjs/common';
import { ApiParam, ApiQuery, ApiTags, ApiBody, ApiConsumes, ApiBearerAuth } from '@nestjs/swagger';
import { BrandRegisterService } from './brand-register.services';
import { toJsonResponse } from '../../common/response';
import { BrandGuard } from '../auth/guards/brand.guard';
import { AdminBrandGuard } from '../auth/guards/admin-brand.guard';

// DTO Files
import { FirstStepDto } from './dtos/first-step.dto';
import { SecondStepDto } from './dtos/second-step.dto';
import { ThirdStepDto } from './dtos/third-step.dto';
import { BrandSlugParamDto } from './dtos/brand-slug-param.dto';
import { BrandUpdateDto } from './dtos/brand-update.dto';

@ApiTags('brand-register')
@Controller('brand-register')
export class BrandRegisterController {
    constructor(
        private brandRegisterService: BrandRegisterService,
    ) { }

    // JP-
    @ApiBearerAuth('access-token')
    @ApiBody({ type: FirstStepDto })
    @UseGuards(BrandGuard)
    @Put('first-step')
    async firstStep(@Body() request: FirstStepDto, @Request() req, @Res() response: any) {
        try {
            const data = await this.brandRegisterService.firstStep(request, req.user);
            response.status(201).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    // JP-
    @ApiBearerAuth('access-token')
    @ApiBody({ type: SecondStepDto })
    @UseGuards(BrandGuard)
    @Put('second-step')
    async secondStep(@Body() request: SecondStepDto, @Request() req, @Res() response: any) {
        try {
            const data = await this.brandRegisterService.secondStep(request, req.user);
            response.status(201).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    // JP-
    @ApiBearerAuth('access-token')
    @ApiBody({ type: ThirdStepDto })
    @UseGuards(BrandGuard)
    @Put('third-step')
    async thirdStep(@Body() request: ThirdStepDto, @Request() req, @Res() response: any) {
        try {
            const data = await this.brandRegisterService.thirdStep(request, req.user);
            response.status(201).json(data);
        } catch (error) {
            console.log(error);
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }

    @ApiBearerAuth("access-token")
    @UseGuards(BrandGuard)
    @Patch("back-step")
    async backStep(@Request() req, @Res() response: any) {
        try {
            const data = await this.brandRegisterService.backStep(req.user)
            response.status(201).json(data)
        } catch (error) {
            console.log(error)
            response.status(error.status).json(toJsonResponse(error.status, error.message))
        }
    }


    // JP-
    @ApiBearerAuth('access-token')
    @ApiParam({ name: 'brand_id', required: true })
    @ApiBody({ type: BrandUpdateDto })
    @UseGuards(AdminBrandGuard)
    @Put('edit/:brand_slug')
    /**
     * Updates the brand data based on the provided request data.
     *
     * @param params - An instance of BrandSlugParamDto containing the brand slug to be updated.
     * @param request - An instance of BrandUpdateDto containing the brand information to be updated.
     * @param response - The response object for sending the HTTP response.
     * @param req - The request object, containing the authenticated user information.
     * Responds with a 201 status if the update is successful.
     * In case of an error, logs the error and responds with the respective error status and message.
     */
    async brandUpdate(@Param() params: BrandSlugParamDto, @Body() request: BrandUpdateDto, @Request() req, @Res() response: any) {
        try {
            const data = await this.brandRegisterService.brandUpdate(params, request, req.user);
            response.status(200).json(data);
        } catch (error) {
            response.status(error.status).json(toJsonResponse(error.status, error.message));
        }
    }
}
