import { Controller, Get, Post, Body, Patch, Param, UseGuards, Delete, Query } from '@nestjs/common';
import { RestaurantsService } from '../providers/restaurants.service';
import { CreateRestaurantDto } from '../dto/create-restaurant.dto';
import { UpdateRestaurantDto } from '../dto/update-restaurant.dto';
import { UpgradeSubscriptionDto } from '../dto/upgrade-subscription.dto';
import { FindRestaurantsDto } from '../dto/find-restaurants.dto';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import {
  ApiBearerAuth,
  ApiBody,
  ApiNotFoundResponse,
  ApiOperation,
  ApiParam,
  ApiTags,
  ApiHeader,
} from '@nestjs/swagger';
import { RestId } from 'src/common/decorators';
import { API_HEADER_SLUG } from 'src/common/constants';
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard';
import { Permissions } from 'src/common/decorators/permissions.decorator';
import { Permission } from 'src/common/enums/permission.enum';

@ApiTags('restaurants')
@Controller()
export class RestaurantsController {
  constructor(private readonly restaurantsService: RestaurantsService) { }

  @Get('public/restaurants/:slug')
  @ApiOperation({ summary: 'Get restaurant specification by slug' })
  @ApiHeader(API_HEADER_SLUG)
  @ApiParam({ name: 'slug', required: true, description: 'Restaurant slug' })
  @ApiNotFoundResponse({ description: 'Restaurant not found' })
  findBySlug(@Param('slug') slug: string) {
    return this.restaurantsService.getRestaurantSpecification(slug);
  }

  /** Admin Endpoints */

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.UPDATE_RESTAURANT)
  @ApiOperation({ summary: 'Get restaurant by ID from request' })
  @Get('admin/restaurants/my-restaurant')
  async findOne(@RestId() restId: string) {
    return await this.restaurantsService.findOneOrFail(restId);
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.UPDATE_RESTAURANT)
  @ApiOperation({ summary: 'Update a restaurant' })
  @ApiBody({ type: UpdateRestaurantDto })
  @Patch('admin/restaurants/my-restaurant')
  updateMyRestaurant(@RestId() restId: string, @Body() updateRestaurantDto: UpdateRestaurantDto) {
    return this.restaurantsService.update(restId, updateRestaurantDto);
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.UPDATE_RESTAURANT)
  @ApiOperation({ summary: 'Delete a restaurant' })
  @Delete('admin/restaurants/:id')
  remove(@Param('id') id: string) {
    return this.restaurantsService.remove(id);
  }

  /** Super Admin Endpoints */
  @UseGuards(SuperAdminAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get all restaurants with pagination and filters' })
  @Get('super-admin/restaurants')
  findAll(@Query() dto: FindRestaurantsDto) {
    return this.restaurantsService.findAll(dto);
  }

  @UseGuards(SuperAdminAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Create a new restaurant' })
  @Post('super-admin/restaurants')
  createRestaurant(@Body() createRestaurantDto: CreateRestaurantDto) {
    return this.restaurantsService.setupRestuarant(createRestaurantDto);
  }

  @UseGuards(SuperAdminAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get restaurant by subscription ID' })
  @ApiParam({ name: 'subscriptionId', required: true, description: 'Subscription ID' })
  @Get('super-admin/restaurants/subscription/:subscriptionId')
  findOneBySubscriptionId(@Param('subscriptionId') subscriptionId: string) {
    return this.restaurantsService.findOneBySubscriptionId(subscriptionId);
  }

  @UseGuards(SuperAdminAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Get restaurant by ID' })
  @ApiParam({ name: 'id', required: true, description: 'Restaurant ID' })
  @Get('super-admin/restaurants/:id')
  findOneById(@Param('id') id: string) {
    return this.restaurantsService.findOneOrFail(id);
  }

  @UseGuards(SuperAdminAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Update a restaurant by ID' })
  @ApiParam({ name: 'id', required: true, description: 'Restaurant ID' })
  @ApiBody({ type: UpdateRestaurantDto })
  @Patch('super-admin/restaurants/:id')
  updateRestaurant(@Param('id') id: string, @Body() updateRestaurantDto: UpdateRestaurantDto) {
    return this.restaurantsService.update(id, updateRestaurantDto);
  }

  @UseGuards(SuperAdminAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Upgrade subscription for a restaurant' })
  @ApiParam({ name: 'subscriptionId', required: true, description: 'Subscription ID' })
  @ApiBody({ type: UpgradeSubscriptionDto })
  @Patch('super-admin/restaurants/subscription/:subscriptionId/upgrade')
  upgradeSubscription(
    @Param('subscriptionId') subscriptionId: string,
    @Body() upgradeSubscriptionDto: UpgradeSubscriptionDto
  ) {
    return this.restaurantsService.upgradeSubscription(subscriptionId, upgradeSubscriptionDto);
  }

  @UseGuards(SuperAdminAuthGuard)
  @ApiBearerAuth()
  @ApiOperation({ summary: 'Hard delete a restaurant and all related data' })
  @ApiParam({ name: 'id', required: true, description: 'Restaurant ID' })
  @Delete('super-admin/restaurants/:id/hard-delete')
  hardDeleteRestaurant(@Param('id') id: string) {
    return this.restaurantsService.hardDeleteRestaurant(id);
  }

}
