// Questo file fa parte di MyTown.
// MyTown è software libero: puoi ridistribuirlo e/o modificarlo secondo
// i termini della GNU Affero General Public License come pubblicata dalla
// Free Software Foundation, versione 3.
// Distribuito senza ALCUNA GARANZIA; vedi la GNU AGPL v3 per i dettagli.
// Copyright (C) Kondor Consulting & Services S.r.l.
// SPDX-License-Identifier: AGPL-3.0-or-later

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

import { ConfigService } from './config.service';
import { WorksiteCmp } from '../models/worksite-cmp.model';

/**
 * Cantieri (ws_worksite) — nuova gestione ws_spring_cmp. Stessi endpoint del web (worksite/*).
 */
@Injectable({ providedIn: 'root' })
export class WorksiteService {
	constructor(
		private http: HttpClient,
		private config: ConfigService,
	) {}

	private get base(): string {
		return this.config.serviceUrl_Companies;
	}

	/** Lista cantieri del condominio (azienda). Ricerca/paginazione server-side. */
	getWorksites(cmp_id: number, txtsearch = '', order_by = '', limit = 50, offset = 1): Observable<WorksiteCmp[]> {
		const url = `${this.base}worksite/list?txtsearch=${encodeURIComponent(txtsearch)}&order_by=${encodeURIComponent(order_by)}&limit=${limit}&offset=${offset}&cmp_id=${cmp_id}&lot_id=0`;
		return this.http.get<WorksiteCmp[]>(url, { observe: 'response' }).pipe(map((r) => r.body || []));
	}

	getWorksite(ws_id: number): Observable<WorksiteCmp> {
		return this.http.get<WorksiteCmp>(`${this.base}worksite/get/${ws_id}`, { observe: 'response' }).pipe(map((r) => r.body as WorksiteCmp));
	}
}
