import {
  Controller,
  Get,
  Param,
  Query,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { FranchiseHistoryService } from './franchise_history.service';
import { Public } from '../auth/public.decorator';
import {
  ApiBearerAuth,
  ApiTags,
  ApiBody,
  ApiParam,
  ApiQuery,
  ApiOperation,
} from '@nestjs/swagger';

@ApiTags('franchiseHistory')
@ApiBearerAuth('access-token')
@Controller('franchiseHistory')
export class FranchiseHistoryController {
  constructor(
    private readonly franchiseHistoryService: FranchiseHistoryService,
  ) {}

  @Public()
  @Get()
  @ApiOperation({ summary: 'Get History' })
  @ApiQuery({ name: 'franchiseId', required: false, type: Number })
  @ApiQuery({ name: 'tempFranchiseId', required: false, type: Number })
  async getHistory(
    @Query('franchiseId') franchiseId?: number,
    @Query('tempFranchiseId') tempFranchiseId?: number,
  ) {
    try {
      const history = await this.franchiseHistoryService.getHistory(
        franchiseId,
        tempFranchiseId,
      );
      return {
        success: true,
        data: history,
      };
    } catch (error) {
      throw new HttpException(
        { message: error.message || 'Error fetching history' },
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  @Public()
  @Get('franchise/:franchiseId')
  @ApiOperation({ summary: 'Get Franchise History' })
  @ApiParam({ name: 'franchiseId', type: Number })
  async getFranchiseHistory(@Param('franchiseId') franchiseId: number) {
    try {
      const history =
        await this.franchiseHistoryService.getFranchiseHistory(franchiseId);
      return {
        success: true,
        data: history,
      };
    } catch (error) {
      throw new HttpException(
        { message: error.message || 'Error fetching franchise history' },
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }

  @Public()
  @Get('application/:tempFranchiseId')
  @ApiOperation({ summary: 'Get Application History' })
  @ApiParam({ name: 'tempFranchiseId', type: Number })
  async getApplicationHistory(
    @Param('tempFranchiseId') tempFranchiseId: number,
  ) {
    try {
      const history =
        await this.franchiseHistoryService.getApplicationHistory(
          tempFranchiseId,
        );
      return {
        success: true,
        data: history,
      };
    } catch (error) {
      throw new HttpException(
        { message: error.message || 'Error fetching application history' },
        HttpStatus.INTERNAL_SERVER_ERROR,
      );
    }
  }
}
