import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Query,
  UseGuards,
  Delete,
} from '@nestjs/common';
import { ContactService } from '../providers/contact.service';
import { CreateContactDto } from '../dto/create-contact.dto';
import { FindContactsDto } from '../dto/find-contacts.dto';
import { UpdateContactStatusDto } from '../dto/update-contact-status.dto';
import { ApiTags, ApiOperation, ApiBearerAuth, ApiParam, ApiQuery, ApiHeader } from '@nestjs/swagger';
import { AdminAuthGuard } from 'src/modules/auth/guards/adminAuth.guard';
import { SuperAdminAuthGuard } from 'src/modules/auth/guards/superAdminAuth.guard';
import { OptionalAuthGuard } from 'src/modules/auth/guards/optinalAuth.guard';
import { API_HEADER_SLUG } from 'src/common/constants';
import { Permission } from 'src/common/enums/permission.enum';
import { Permissions } from 'src/common/decorators/permissions.decorator';
import { RestId } from 'src/common/decorators/rest-id.decorator';
import { RestSlug } from 'src/common/decorators/rest-slug.decorator';

@ApiTags('contact')
@Controller()
export class ContactController {
  constructor(private readonly contactService: ContactService) { }

  @UseGuards(OptionalAuthGuard)
  @Post('public/contact')
  @ApiOperation({ summary: 'Create a new contact (public endpoint)' })
  @ApiHeader(API_HEADER_SLUG)
  async create(@Body() createContactDto: CreateContactDto, @RestSlug() slug: string, @RestId() restId?: string) {
    return this.contactService.create(createContactDto, restId, slug);
  }


  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_CONTACTS)
  @Get('admin/contact')
  @ApiOperation({ summary: 'Get paginated list of contacts (admin only)' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiQuery({ name: 'search', required: false, type: String })
  @ApiQuery({ name: 'orderBy', required: false, type: String })
  @ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
  @ApiQuery({ name: 'status', required: false, enum: ['new', 'seen'] })
  async findAllPaginated(@RestId() restId: string, @Query() dto: FindContactsDto) {
    return this.contactService.findAllPaginatedAdmin(dto, restId);
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_CONTACTS)
  @Get('admin/contact/:id')
  @ApiOperation({ summary: 'Get contact details by ID (admin only)' })
  @ApiParam({ name: 'id', description: 'Contact ID' })
  async findOne(@RestId() restId: string, @Param('id') id: string) {
    return this.contactService.findOne(id, restId);
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_CONTACTS)
  @Patch('admin/contact/:id/status')
  @ApiOperation({ summary: 'Update contact status (admin only)' })
  @ApiParam({ name: 'id', description: 'Contact ID' })
  async updateStatus(@RestId() restId: string, @Param('id') id: string, @Body() dto: UpdateContactStatusDto) {
    return this.contactService.updateStatus(id, dto, restId);
  }

  @UseGuards(AdminAuthGuard)
  @ApiBearerAuth()
  @Permissions(Permission.MANAGE_CONTACTS)
  @Delete('admin/contact/:id')
  @ApiOperation({ summary: 'Hard delete a contact (admin only)' })
  @ApiParam({ name: 'id', description: 'Contact ID' })
  async remove(@RestId() restId: string, @Param('id') id: string) {
    await this.contactService.remove(id, restId);
  }

  /*** Super Admin ***/
  @UseGuards(SuperAdminAuthGuard)
  @ApiBearerAuth()
  @Get('super-admin/contact')
  @ApiOperation({ summary: 'Get paginated list of contacts (admin only)' })
  @ApiQuery({ name: 'page', required: false, type: Number })
  @ApiQuery({ name: 'limit', required: false, type: Number })
  @ApiQuery({ name: 'search', required: false, type: String })
  @ApiQuery({ name: 'orderBy', required: false, type: String })
  @ApiQuery({ name: 'order', required: false, enum: ['asc', 'desc'] })
  @ApiQuery({ name: 'status', required: false, enum: ['new', 'seen'] })
  async findAllPaginatedForSuper(@Query() dto: FindContactsDto) {
    return this.contactService.findAllPaginatedSuper(dto);
  }

  @UseGuards(SuperAdminAuthGuard)
  @ApiBearerAuth()
  @Get('super-admin/contact/:id')
  @ApiOperation({ summary: 'Get contact details by ID (admin only)' })
  @ApiParam({ name: 'id', description: 'Contact ID' })
  async findOneForSuper(@Param('id') id: string) {
    return this.contactService.findOne(id);
  }
}
