import { Injectable } from '@angular/core';
import { ApiService } from '../api.service';
import { map, Observable } from 'rxjs';

import {
  CHANGE_PASSWORD,
  CHANGE_PASSWORD_RESPONSE,
  FORGET_PASSWORD,
  FORGET_PASSWORD_RESPONSE,
  LOGIN,
  LOGIN_RESPONSE,
  OTP,
  OTP_RESPONSE,
  Profile,
  QUALIFICATION,
  JOB_TITLE,
  UNIVERISTY,
  COUNTRY,
  INDUSTRY,
  CITY,
} from '@core/models/authenticcation.model';

@Injectable({
  providedIn: 'root',
})
export class AuthService {
  constructor(private readonly apiService: ApiService) {}

  Login(data: LOGIN): Observable<LOGIN_RESPONSE> {
    return this.apiService.post<LOGIN, LOGIN_RESPONSE>('Auth/login', data);
  }

  getUserData(): Observable<Profile> {
    return this.apiService.get<Profile>('User/Get', {
      userId: localStorage.getItem('id'),
    });
  }

  ForgetPassword(
    params: FORGET_PASSWORD
  ): Observable<FORGET_PASSWORD_RESPONSE> {
    return this.apiService.get<FORGET_PASSWORD_RESPONSE>(
      'Auth/forgetPasswordMail',
      params
    );
  }

  SendCode(data: OTP): Observable<OTP_RESPONSE> {
    return this.apiService.post<OTP, OTP_RESPONSE>('Auth/VerifyOTP', data);
  }

  ChnagePassword(data: CHANGE_PASSWORD): Observable<CHANGE_PASSWORD_RESPONSE> {
    return this.apiService.post<CHANGE_PASSWORD, CHANGE_PASSWORD_RESPONSE>(
      'Auth/forgetPassword',
      data
    );
  }

  getJobtitles(): Observable<JOB_TITLE[]> {
    return this.apiService.get<JOB_TITLE[]>('Lookup/GetAllJobTitles');
  }

  getQualifications(): Observable<QUALIFICATION[]> {
    return this.apiService.get<QUALIFICATION[]>('Lookup/GetAllQualifications');
  }

  getUniversity(): Observable<UNIVERISTY[]> {
    return this.apiService.get<UNIVERISTY[]>('Lookup/GetAllUniversities');
  }

  getCountries(): Observable<COUNTRY[]> {
    return this.apiService.get<COUNTRY[]>('Lookup/GetAllCountries');
  }

  addEmployee(data: any): Observable<any> {
    return this.apiService.post<any, any>('Auth/register', data);
  }

  getIndustries(): Observable<INDUSTRY[]> {
    return this.apiService.get<INDUSTRY[]>('Lookup/GetAllIndustries');
  }

  getCities(params: { CountryId: number }): Observable<CITY[]> {
    return this.apiService.get<CITY[]>('Lookup/GetCityByCountry', params);
  }

  createCompany(data: any): Observable<any> {
    return this.apiService.post<any, Observable<any>>('Company/Create', data);
  }

  isAuthenticated(): boolean {
    return !!(localStorage.getItem('token') && localStorage.getItem('id'));
  }
}
