import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable,} from 'rxjs';
import { environment } from 'src/environments/environment';
import { map } from 'rxjs/operators';

export interface LotComment {
  jobid: number;
  comment_text: string;
  createdon: string;
  projectdefinitionid: number;
  jobtitle: string;
  querytype: string;
  status: string;
    cid: number;   
}

@Injectable({
  providedIn: 'root'
})
export class LotCommentService {
  private baseUrl = environment.serviceUrl;

  constructor(private http: HttpClient) {}

  getCommentsByProject(projectDefinitionId: number): Observable<LotComment[]> {
    const body = { projectdefinitionid: projectDefinitionId };
    
    return this.http
      .post<any[]>(`${this.baseUrl}api/DashboardApi/getLotCommentsByProject`, body)
      .pipe(
        map((res: any) => {
    
          if (Array.isArray(res) && typeof res[0] === 'string') {
            try {
              return JSON.parse(res[0]) as LotComment[];
            } catch (e) {
              console.error('JSON parse error in getCommentsByProject:', e);
              return [];
            }
          }
       
          return res;
        })
      );
  }

updateQueryType(jobid: number, querytype: string): Observable<any> {
  const body = { jobid, querytype, mode: 'updateQueryType' };
  return this.http.post(`${this.baseUrl}api/DashboardApi/updateLotComment`, body);
}

updateStatus(jobid: number, status: string): Observable<any> {
  const body = { jobid, status, mode: 'updateStatus' };
  return this.http.post(`${this.baseUrl}api/DashboardApi/updateLotComment`, body);
}

updateStartEndpage(queryid: number, start: number, end: number, mode:string): Observable<any> {
  const body = { cid:queryid, startpage:start, endpage:end, mode };
  return this.http.post(`${this.baseUrl}api/DashboardApi/updateLotComment`, body);
}


}
