import { Injectable } from '@angular/core';

import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';

import { Observable } from 'rxjs';
import { environment } from 'src/app/environments/environment';
import { DnssecDetails } from 'src/app/model/DnsSecDetails.model';

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

    private dnsSecUrl =  environment.apiURL+'/dr/dnssec';

  constructor(private http: HttpClient) {}

    saveAllDnsSecRecords(dnsSecList: any): Observable<HttpResponse<any[]>> {
        return this.http.post<any[]>(`${this.dnsSecUrl}/saveAll`,dnsSecList, {
            observe: 'response', headers: new HttpHeaders({
                'Authorization': 'Bearer ' + localStorage.getItem('jwtToken')
            })
        });
    }

     updateAllDnsSecRecords(dnsSecList: any): Observable<HttpResponse<any[]>> {
        return this.http.put<any[]>(`${this.dnsSecUrl}/updateAll`,dnsSecList, {
            observe: 'response', headers: new HttpHeaders({
                'Authorization': 'Bearer ' + localStorage.getItem('jwtToken')
            })
        });
    }

    getDnsSecByDomainId(domainId: number){
        return this.http.get<any[]>(`${this.dnsSecUrl}/getDnsSecByDomainId/${domainId}`, {
            observe: 'response', headers: new HttpHeaders({
                'Authorization': 'Bearer ' + localStorage.getItem('jwtToken')
            })
        });
    }

      getDnsAlgorithmTypes(){
        return this.http.get<any[]>(`${this.dnsSecUrl}/algorithmTypes`, {
            observe: 'response', headers: new HttpHeaders({
                'Authorization': 'Bearer ' + localStorage.getItem('jwtToken')
            })
        });
    }

     getDnsDigestTypes(){
        return this.http.get<any[]>(`${this.dnsSecUrl}/digestTypes`, {
            observe: 'response', headers: new HttpHeaders({
                'Authorization': 'Bearer ' + localStorage.getItem('jwtToken')
            })
        });
    }

    sendDnsSecToNixi(dnsSec: DnssecDetails, domainId: number): Observable<HttpResponse<any>> {
    const token = localStorage.getItem('jwtToken'); // or get from AuthService
    const headers = new HttpHeaders({
      'Authorization': `Bearer ${token}`,
    });

    return this.http.post<any>(
      `${this.dnsSecUrl}/sendDnsSecToNixi/${domainId}`,
      dnsSec,
      {
        headers,
        observe: 'response' 
      }
    );
  }


  deleteDnsSecById(dnsSecId: number): Observable<HttpResponse<any>> {
    const token = localStorage.getItem('jwtToken'); // or get from AuthService
    const headers = new HttpHeaders({
      'Authorization': `Bearer ${token}`,
    });

    return this.http.delete<any>(
      `${this.dnsSecUrl}/deleteDnsSecById/${dnsSecId}`,
      {
        headers,
        observe: 'response'  
      }
    );
  }

  deleteDnsSecByIdOnApproval(isDeletionApproved: boolean, dnsSecId: number): Observable<HttpResponse<any>> {
    const token = localStorage.getItem('jwtToken'); // or get from AuthService
    const headers = new HttpHeaders({
      'Authorization': `Bearer ${token}`,
    });

    return this.http.delete<any>(
      `${this.dnsSecUrl}/deleteDnsSecById/${isDeletionApproved}/${dnsSecId}`,
      {
        headers,
        observe: 'response'  
      }
    );
  }

  sendDeleteRequest(toBeDeleted: boolean, id: number): Observable<HttpResponse<any>> {
    const token = localStorage.getItem('jwtToken'); // or get from AuthService
    const headers = new HttpHeaders({
      'Authorization': `Bearer ${token}`,
    });

    return this.http.put<any>(
      `${this.dnsSecUrl}/sendDeleteRequest/${toBeDeleted}/${id}`,
      {},
      {
        headers,
        observe: 'response'  
      }
    );
  }



}