import {
  Controller,
  Get,
  Post,
  Put,
  Patch,
  Delete,
  Body,
  Param,
  Query,
  UseGuards,
  Request,
  HttpCode,
  HttpStatus,
} from '@nestjs/common';
import { LegalPagesService } from './legal-pages.service';
import { AdminGuard } from '../../../common/guards/admin.guard';
import { Public } from '../../auth/decorators/public.decorator';
import {
  CreateLegalPageDto,
  UpdateLegalPageDto,
  PublishLegalPageDto,
  RevertToVersionDto,
  LegalPageListFilter,
} from './legal-pages.dto';

@Controller('api/v1/admin/legal-pages')
@UseGuards(AdminGuard)
export class LegalPagesController {
  constructor(private readonly legalPagesService: LegalPagesService) {}

  @Get()
  async findAll(@Query() filter: LegalPageListFilter) {
    const pages = await this.legalPagesService.findAll(filter);
    return pages.map(page => this.legalPagesService.toResponse(page));
  }

  @Get(':id')
  async findOne(@Param('id') id: string) {
    const page = await this.legalPagesService.findOne(id);
    return this.legalPagesService.toResponse(page);
  }

  @Post()
  async create(@Body() dto: CreateLegalPageDto, @Request() req) {
    const userId = req.user?.id;
    const page = await this.legalPagesService.create(dto, userId);
    return this.legalPagesService.toResponse(page);
  }

  @Put(':id')
  async update(@Param('id') id: string, @Body() dto: UpdateLegalPageDto, @Request() req) {
    const userId = req.user?.id;
    const page = await this.legalPagesService.update(id, dto, userId);
    return this.legalPagesService.toResponse(page);
  }

  @Patch(':id/publish')
  async publish(@Param('id') id: string, @Body() dto: PublishLegalPageDto, @Request() req) {
    const userId = req.user?.id;
    const page = await this.legalPagesService.publish(id, dto.isPublished, userId);
    return this.legalPagesService.toResponse(page);
  }

  @Delete(':id')
  @HttpCode(HttpStatus.NO_CONTENT)
  async delete(@Param('id') id: string) {
    await this.legalPagesService.delete(id);
  }

  @Get(':id/versions')
  async getVersionHistory(@Param('id') id: string) {
    const versions = await this.legalPagesService.getVersionHistory(id);
    return versions.map(version => this.legalPagesService.toVersionResponse(version));
  }

  @Get(':id/versions/:version')
  async getVersion(@Param('id') id: string, @Param('version') version: number) {
    const versionData = await this.legalPagesService.getVersionById(id, version);
    return this.legalPagesService.toVersionResponse(versionData);
  }

  @Post(':id/revert')
  async revertToVersion(
    @Param('id') id: string,
    @Body() dto: RevertToVersionDto,
    @Request() req,
  ) {
    const userId = req.user?.id;
    const page = await this.legalPagesService.revertToVersion(id, dto.version, userId);
    return this.legalPagesService.toResponse(page);
  }
}

// Public endpoint for reading published legal pages
@Controller('api/v1/legal-pages')
export class PublicLegalPagesController {
  constructor(private readonly legalPagesService: LegalPagesService) {}

  @Public()
  @Get(':slug')
  async getPublishedPage(
    @Param('slug') slug: string,
    @Query('language') language: string = 'en',
  ) {
    const page = await this.legalPagesService.findBySlugAndLanguage(slug, language);

    if (!page) {
      return {
        error: 'Page not found',
        message: `Legal page "${slug}" is not available in language "${language}"`,
      };
    }

    return this.legalPagesService.toResponse(page);
  }
}
