import {
  Controller,
  Get,
  HttpException,
  HttpStatus,
  Query,
} from '@nestjs/common';
import { SitemapService } from './sitemap.service';
import { Public } from '../auth/public.decorator';
import { ApiTags, ApiOperation } from '@nestjs/swagger';

@ApiTags('sitemap')
@Controller('sitemap')
export class SitemapController {
  constructor(private readonly sitemapService: SitemapService) {}

  @Public()
  @Get('jobs')
  @ApiOperation({ summary: 'Get sitemap jobs (Full or Paginated)' })
  async getJobs(
    @Query('page') page?: string,
    @Query('limit') limit: string = '200',
  ) {
    try {
      if (page !== undefined) {
        const jobs = await this.sitemapService.getPaginatedActiveJobs(
          parseInt(page),
          parseInt(limit),
        );
        return { jobs };
      }
      return await this.sitemapService.getSitemapJobs();
    } catch (error) {
      throw new HttpException('Failed to fetch jobs', 500);
    }
  }

  @Public()
  @Get('jobs/count')
  @ApiOperation({ summary: 'Get active jobs count' })
  async getActiveJobsCount() {
    try {
      const count = await this.sitemapService.getActiveJobsCount();
      return { count };
    } catch (error) {
      throw new HttpException('Failed to fetch count', 500);
    }
  }

  @Public()
  @Get('jobs/expired/count')
  @ApiOperation({ summary: 'Get expired jobs count' })
  async getExpiredJobsCount() {
    try {
      const count = await this.sitemapService.getExpiredJobsCount();
      return { count };
    } catch (error) {
      throw new HttpException('Failed to fetch count', 500);
    }
  }

  @Public()
  @Get('jobs/expired')
  @ApiOperation({ summary: 'Get paginated expired jobs' })
  async getExpiredPaginated(
    @Query('page') page: string = '0',
    @Query('limit') limit: string = '200',
  ) {
    try {
      const jobs = await this.sitemapService.getPaginatedExpiredJobs(
        parseInt(page),
        parseInt(limit),
      );
      return { jobs };
    } catch (error) {
      throw new HttpException('Failed to fetch paginated expired jobs', 500);
    }
  }

  @Public()
  @Get('companies')
  @ApiOperation({ summary: 'Get sitemap companies' })
  async getCompanies() {
    try {
      return await this.sitemapService.getSitemapCompanies();
    } catch (error) {
      throw new HttpException(
        'Failed to fetch companies for sitemap',
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }
}
