import {
  CanActivate,
  ExecutionContext,
  Injectable,
  UnauthorizedException,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import { JwtService } from '@nestjs/jwt';
import { Request } from 'express';
import { AuthService } from 'src/auth/auth.service';
import { jwtConstants } from 'src/constants';

import { Reflector } from '@nestjs/core';
import { DbService } from 'src/db/db.service';
import { UsersType } from '../role/user.role';

@Injectable()
export class AuthGuard implements CanActivate {
  constructor(
    private model: DbService,
    private authService: AuthService,
    private jwtService: JwtService,
    private reflector: Reflector,
  ) { }

  async canActivate(context: ExecutionContext): Promise<boolean> {
    const request = context.switchToHttp().getRequest();
    const language = request.headers['language'];

    const token = this.extractTokenFromHeader(request);

    if (!token) {
      throw new HttpException(
        { error_code: 'unauthorized', error_description: 'unauthorized' },
        HttpStatus.UNAUTHORIZED,
      );
    };


    try {
      const payload = this.jwtService.verify(token, {
        secret: jwtConstants.secret,
      });

      // Check if the token is present in the session table
      const session = await this.authService.checkToken(token);

      if (!session && payload.purpose !== 'register') {
        throw new HttpException(
          { error_code: 'unauthorized', error_description: 'unauthorized' },
          HttpStatus.UNAUTHORIZED,
        );
      }
      let { user_id, type = UsersType.Customer, scope = UsersType.Customer } = payload;

      let user_info = null;
      let fleet_id = null;

      if (type === UsersType.Fleet || scope === UsersType.Fleet || type === UsersType.FleetStaff || scope === UsersType.FleetStaff) {
        user_info = await this.model.Fleet.findOne({ _id: user_id });
        if (user_info?.type === UsersType.FleetStaff) {
          fleet_id = user_info?.owner;
        } else {
          fleet_id = user_info?._id;
        }
      } else {
        user_info = await this.model.customers.findOne({ _id: user_id })
      }

      if (scope === 'company') {
        user_info = await this.model.CompanyModel.findOne({ _id: user_id })
      }

      request.payload = payload;
      request.fleet_id = fleet_id;
      request.user = user_info;
      request.token = token;

    } catch {
      throw new HttpException(
        { error_code: 'unauthorized', error_description: 'unauthorized' },
        HttpStatus.UNAUTHORIZED,
      );
    }
    return true;
  }

  public extractTokenFromHeader(request: Request): string | undefined {
    const [type, token] = request.headers.authorization?.split(' ') ?? [];
    return type === 'Bearer' ? token : undefined;
  }
}
