import { Controller, Post, Get, Body, Param } from '@nestjs/common';
import { Public } from '../auth/public.decorator';
import { JobViewsService } from './job_views.service';
import {
  ApiBearerAuth,
  ApiTags,
  ApiOperation,
  ApiBody,
  ApiParam,
} from '@nestjs/swagger';

@ApiTags('job_views')
@ApiBearerAuth('access-token')
@Controller('job_views')
export class JobViewsController {
  constructor(private readonly jobViewsService: JobViewsService) {}

  @Public()
  @Post('track')
  @ApiOperation({ summary: 'Track job view' })
  @ApiBody({
    schema: {
      type: 'object',
      properties: {
        jobPostId: { type: 'number' },
        viewerId: { type: 'number' },
        userRole: { type: 'string' },
      },
      required: ['jobPostId'],
    },
  })
  async trackJobView(
    @Body() data: { jobPostId: number; viewerId?: number; userRole?: string },
  ) {
    return await this.jobViewsService.trackJobView(
      data.jobPostId,
      data.viewerId,
      data.userRole,
    );
  }

  @Public()
  @Get('count/:jobPostId')
  @ApiOperation({ summary: 'Get job view count' })
  @ApiParam({ name: 'jobPostId', type: String })
  async getJobViewsCount(@Param('jobPostId') jobPostId: string) {
    return await this.jobViewsService.getJobViewsCount(parseInt(jobPostId));
  }

  @Public()
  @Get('company/:companyId')
  @ApiOperation({ summary: 'Get job views by company' })
  @ApiParam({ name: 'companyId', type: String })
  async getJobViewsByCompany(@Param('companyId') companyId: string) {
    return await this.jobViewsService.getJobViewsByCompany(parseInt(companyId));
  }

  @Public()
  @Get('franchise/:franchiseId')
  @ApiOperation({ summary: 'Get job views by franchise' })
  @ApiParam({ name: 'franchiseId', type: String })
  async getJobViewsByFranchise(@Param('franchiseId') franchiseId: string) {
    return await this.jobViewsService.getJobViewsByFranchise(
      parseInt(franchiseId),
    );
  }

  @Public()
  @Get('franchise/:franchiseId/this_month')
  @ApiOperation({ summary: 'Get this month job views by franchise' })
  @ApiParam({ name: 'franchiseId', type: String })
  async getThisMonthViewsByFranchise(
    @Param('franchiseId') franchiseId: string,
  ) {
    return await this.jobViewsService.getThisMonthViewsByFranchise(
      parseInt(franchiseId),
    );
  }

  @Public()
  @Get('debug/recent/:jobPostId')
  @ApiOperation({ summary: 'Get recent views for job (debug)' })
  @ApiParam({ name: 'jobPostId', type: String })
  async getRecentViewsForJob(@Param('jobPostId') jobPostId: string) {
    return await this.jobViewsService.getRecentViewsForJob(parseInt(jobPostId));
  }
}
