// 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 { CondoUnit, CondoUnitMember } from '../models/condo-unit.model';

/**
 * Unità immobiliari del condominio — stesso backend del portale web (ws_spring_cmp).
 * Admin: tutte le unità; condomino: solo le sue (bycustomer) con referente modificabile.
 */
@Injectable({ providedIn: 'root' })
export class CondoUnitsService {
	constructor(
		private http: HttpClient,
		private config: ConfigService,
	) {}

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

	getCondoUnits(cmp_id: number, txtsearch = '', order_by = '', limit = 200, offset = 1): Observable<CondoUnit[]> {
		const url = `${this.base}condounits/list?cmp_id=${cmp_id}&txtsearch=${encodeURIComponent(txtsearch)}&order_by=${encodeURIComponent(order_by)}&limit=${limit}&offset=${offset}`;
		return this.http.get<CondoUnit[]>(url, { observe: 'response' }).pipe(map((r) => r.body || []));
	}

	addUpdCondoUnit(u: CondoUnit): Observable<number> {
		const url = u.unit_id > 0 ? `${this.base}condounits/upd` : `${this.base}condounits/add`;
		return this.http.post<number>(url, u, { observe: 'response' }).pipe(map((r) => r.body as number));
	}

	delCondoUnit(unit_id: number): Observable<any> {
		return this.http.delete(`${this.base}condounits/del/${unit_id}`, { observe: 'response' }).pipe(map((r) => r.body));
	}

	/** Unità di cui il condomino è membro ("le mie unità"). */
	getCondoUnitsByCustomer(cst_id: number): Observable<CondoUnit[]> {
		return this.http.get<CondoUnit[]>(`${this.base}condounits/bycustomer?cst_id=${cst_id}`, { observe: 'response' }).pipe(map((r) => r.body || []));
	}

	/** Membri di una unità (per scelta referente). */
	getCondoUnitMembers(unit_id: number): Observable<CondoUnitMember[]> {
		return this.http.get<CondoUnitMember[]>(`${this.base}condounits/members?unit_id=${unit_id}`, { observe: 'response' }).pipe(map((r) => r.body || []));
	}

	/** Cambio referente: il referente rappresenta l'unità in assemblea. */
	setCondoUnitReferente(unit_id: number, cst_id: number): Observable<any> {
		return this.http.post(`${this.base}condounits/setreferente?unit_id=${unit_id}&cst_id=${cst_id}`, null, { observe: 'response' }).pipe(map((r) => r.body));
	}

	/** Referenti del condominio (solo cst_id) — uso interno. */
	getCondoReferenti(cmp_id: number): Observable<any[]> {
		return this.http.get<any[]>(`${this.base}condounits/referenti?cmp_id=${cmp_id}`, { observe: 'response' }).pipe(map((r) => r.body || []));
	}

	/** Clienti/condòmini del condominio CON nome (per gli invitati delle assemblee). */
	getCustomers(cmp_id: number): Observable<any[]> {
		return this.http.get<any[]>(`${this.base}customers/list?txtsearch=&order_by=&limit=1000&offset=1&cmp_id=${cmp_id}`, { observe: 'response' }).pipe(map((r) => r.body || []));
	}
}
