import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { URL } from '../mocks/url';

@Injectable({
  providedIn: 'root'
})
export class AuthService {

    constructor(
        private http: HttpClient
    ) { }

    authenticateUser(credentials: { username: string, password: string, login_type: string }) {
        return this.http.post(URL + '/api/auth/signin/', credentials).toPromise();
    }

    forgotPassword(email: string) {
        return this.http.post(URL + '/forgot', { email: email }).toPromise();
    }

    resetPasswordForEmail(data: any) {
        return this.http.post(URL + '/reset', data).toPromise();
    }

    validateOtpForForgotPassword(data: { email: string, otp: number | string }) {
        return this.http.post(URL + '/api/auth/otpvalidate', data).toPromise();
    }

    validateOtpForEmail(data: { email: string, otp: number | string }) {
        return this.http.post(URL + '/api/otp/validate', data).toPromise();
    }   

    signupUser(credentials: any) {
        return this.http.post(URL + '/api/auth/signup/', credentials).toPromise();
    }

    updateUser(userData: any) {
        const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + localStorage.access_Token
			})
        };
        
        return this.http.put(URL + '/api/maioraservice/user/v1/update', userData, httpOptions).toPromise();
    }

    getUserDetails(userId: string) {
        const httpOptions = {
			headers: new HttpHeaders({
				'Access-Control-Allow-Origin': '*',
				'Content-Type': 'application/json',
				'Authorization': 'Bearer ' + localStorage.access_Token
			})
        };
        
        return this.http.get(URL + '/api/maioraservice/user/v1/user_id/' + userId + '/', httpOptions).toPromise();
    }
}
