import { Injectable } from '@angular/core';
import { delay, Observable, of } from "rxjs";
import { HttpClient, HttpHeaders, HttpParams, } from "@angular/common/http";
import { ApiRequestService } from "src/app/services/api/api-request.service";
import { environment } from 'src/environments/environment';
@Injectable({
  providedIn: 'root'
})
export class SiteTreeservice {
  private baseURL = "SiteTree/SiteTree";
  private dlfbaseURL = environment.nodeUrl + "/entityBuilder";
  private llmURL = environment.nodeUrl + "/llm";
  private geminiURL = environment.ollamaUrl;


  constructor(
    private http: HttpClient,
    private apiRequest: ApiRequestService,

  ) { }
  getAll(page?: number, size?: number): Observable<any> {
    return this.apiRequest.get(this.baseURL);
  }
  getById(id: number): Observable<any> {
    const _http = this.baseURL + "/" + id;
    return this.apiRequest.get(_http);
  }
  create(data: any): Observable<any> {
    return this.apiRequest.post(this.baseURL, data);
  }
  update(id: number, data: any): Observable<any> {
    const _http = this.baseURL + "/" + id;
    return this.apiRequest.put(_http, data);
  }
  delete(id: number): Observable<any> {
    const _http = this.baseURL + "/" + id;
    return this.apiRequest.delete(_http);
  }


  getDlf(headerId: number, operationType: string, fieldtype: string): Observable<any> {
    const _http = this.dlfbaseURL + "/flf/flf-line/unique?headerId=" + headerId + "&operationType=" + operationType + "&fieldType=" + fieldtype;
    return this.http.get(_http);
  }

  getDesignLibrary(operationType: string, fieldtype: string): Observable<any> {
    const _http = "Design_lbrary/Design_lbrary/random?" + "operationType=" + operationType + "&fieldType=" + fieldtype;
    return this.apiRequest.get(_http);
  }

  getDesignLibraryByheader(headerId: number, operationType: string, fieldtype: string): Observable<any> {
    const _http = "Design_lbrary/Design_lbrary/line/unique?headerId=" + headerId + "&operationType=" + operationType + "&fieldType=" + fieldtype;
    return this.apiRequest.get(_http);
  }

  callLlm(data: any): Observable<any> {
    return this.http.post(`${this.llmURL}/chatMemory`, data);
  }

  generateHtml(data: any): Observable<any> {
    return this.apiRequest.post(`api/html/generate`, data);
  }

  generateHtmlwithcss(id: any): Observable<any> {
    return this.apiRequest.get(`api/html/generate/html/id/` + id);
  }

  deployProj(projId: number, data: any): Observable<any> {
    return this.apiRequest.post(`sureops/deploy?projId=` + projId, data);
  }

  deploySiteBuilder(siteId: number, siteName: string, data: any): Observable<any> {
    const formattedSiteName = this.formatSiteName(siteName);
    const _http = `${this.baseURL}/deploy?siteId=${siteId}&siteBuilderName=${formattedSiteName}`;
    return this.apiRequest.post(_http, data);
  }


  createHtmlfile(siteId: number, siteName: string, data: any): Observable<any> {
    const formattedSiteName = this.formatSiteName(siteName);
    const _http = `${this.baseURL}/createFile?siteId=${siteId}&siteBuilderName=${formattedSiteName}`;
    return this.apiRequest.post(_http, data);
  }

  readPages(siteName: string, pageName: string): Observable<any> {
    const formattedSite = this.formatSiteName(siteName);
    const _http = `${this.baseURL}/read?siteBuilderName=${formattedSite}&filename=${pageName}`;
    return this.apiRequest.get(_http, undefined, 'text');
  }



  createFiles(data: any, projId: number): Observable<any> {
    return this.apiRequest.post(`sureops/createFile?projId=` + projId, data);
  }

  private formatSiteName(name: string): string {
    return name
      .replace(/([a-z])([A-Z])/g, '$1_$2') // Convert camelCase to snake_case
      .replace(/\s+/g, '_') // Replace spaces with underscores
      .toLowerCase();
  }

  // readPages(siteNmae: String, pageNmae: String): Observable<any> {

  //   const _http = this.baseURL + "/read?siteBuilderName=" + siteNmae + '&filename=' + pageNmae;

  //   return this.apiRequest.get(_http, undefined, 'text');
  // }
  callGemini(data: any): Observable<any> {
    return this.http.post(`${this.llmURL}/api/gemini`, data);
  }




  uploadLogoupload(ref: any, Sitebuilder: any, file: any): Observable<any> {
    const formData = new FormData();
    formData.append('file', file);
    return this.apiRequest.postFormData(`FileUpload/Uploadeddocs/${ref}/${Sitebuilder}`, formData);
  }

  uploadLogouploadgetById(ref: any, Sitebuilder: any,): Observable<any> {
    return this.apiRequest.get(`FileUpload/Uploadeddocs/${ref}/${Sitebuilder}`);
  }


  uploadLogouploaddelete(id: number): Observable<any> {
    return this.apiRequest.delete(`FileUpload/Uploadeddocs/${id}`);
  }
  // updateactionƒ
}