// 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 { Review, ReviewSource, ReviewStats } from 'src/models/biz-appointments/review.model';

@Injectable({
	providedIn: 'root',
})
export class BizReviewsService {
	constructor(private http: HttpClient, private configService: ConfigService) {}

	/** Recensioni centralizzate del profilo impresa: backend ws_spring_cmp. */
	private get baseUrl(): string {
		return this.configService.serviceUrl_Companies;
	}

	/** Eligibilita + recensione esistente per una transazione sorgente. */
	getEligible(source: ReviewSource, source_id: number): Observable<{ eligible: boolean; existing: Review | null }> {
		return this.http.get(`${this.baseUrl}reviews/eligible?source=${source}&source_id=${source_id}`, { observe: 'response' }).pipe(map((response: any) => response.body));
	}

	/** Invia una recensione (1 per transazione completata; rev_source/rev_source_id nel body). */
	addReview(review: Partial<Review>): Observable<number> {
		return this.http.post(`${this.baseUrl}reviews/add`, review, { observe: 'response' }).pipe(map((response: any) => response.body));
	}

	/** Recensioni pubblicate di un'impresa (filtri opz. per tipo servizio / sorgente).
	 *  Senza sorgente le LUOGO sono escluse dal backend; con source='ORDINE' si
	 *  ottengono solo le recensioni del negozio online. */
	getReviews(cmp_id: number, service_type_id?: number | null, limit: number = 20, offset: number = 0, source?: ReviewSource | null): Observable<{ list: Review[]; totPagine: number; totalRows: number }> {
		let url = `${this.baseUrl}reviews/list?cmp_id=${cmp_id}&limit=${limit}&offset=${offset}`;
		if (service_type_id) url += `&service_type_id=${service_type_id}`;
		if (source) url += `&source=${source}`;
		return this.http.get(url, { observe: 'response' }).pipe(map((response: any) => response.body));
	}

	/** Statistiche aggregate del profilo impresa. */
	getStats(cmp_id: number): Observable<ReviewStats> {
		return this.http.get(`${this.baseUrl}reviews/stats?cmp_id=${cmp_id}`, { observe: 'response' }).pipe(map((response: any) => response.body));
	}

	/** Segnala una recensione. */
	reportReview(rev_id: number, reason: string, notes?: string): Observable<number> {
		return this.http.post(`${this.baseUrl}reviews/${rev_id}/report`, { reason, notes: notes || '' }, { observe: 'response' }).pipe(map((response: any) => response.body));
	}

	/** Recensioni + statistiche di un luogo turismo (una sola chiamata). */
	getPlaceReviews(plc_id: number, limit: number = 20, offset: number = 0): Observable<{ list: Review[]; totPagine: number; totalRows: number; stats: ReviewStats }> {
		return this.http.get(`${this.baseUrl}reviews/list-place?plc_id=${plc_id}&limit=${limit}&offset=${offset}`, { observe: 'response' }).pipe(map((response: any) => response.body));
	}
}
