import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { Observable } from 'rxjs';
import { ApiService } from './api.service';
import { AuthService } from './auth.service';

// Interfaccia per tipizzare i dati della struttura
export interface Structure {
  id: number;
  name: string;
  address?: string | null;
  city?: string | null;
  province?: string | null;
  zip_code?: string | null;
  phone?: string | null;
  email?: string | null;
  notes?: string | null;
  created_at: string; // O Date se preferisci fare il parsing
  updated_at: string; // O Date
}

@Injectable({
  providedIn: 'root'
})
export class StructureService extends ApiService {

  constructor(authService: AuthService, http: HttpClient) {
      super(authService, http, 'structures');
  }

  // GET /api/structures
  getStructures(): Observable<Structure[]> {
    return this.get<Structure[]>(`${this.controllerName}`);
  }

  // GET /api/structures/{id}
  getStructure(id: number): Observable<Structure> {
    return this.get<Structure>(`${this.controllerName}/${id}`);
  }

  // POST /api/structures
  addStructure(structure: Partial<Structure>): Observable<Structure> {
    return this.post<Structure>(`${this.controllerName}`, structure);
  }

  // PUT /api/structures/{id}
  updateStructure(id: number, structure: Partial<Structure>): Observable<Structure> {
    return this.put<Structure>(`${this.controllerName}/${id}`, structure);
  }

  // DELETE /api/structures/{id}
  deleteStructure(id: number): Observable<null> { // DELETE restituisce 204 No Content
    // Specificare responseType: 'text' può aiutare se il backend non restituisce JSON valido su DELETE
    // ma dato che restituisce 204, Observable<null> è appropriato.
    return this.delete<null>(`${this.controllerName}/${id}`);
  }
}
