import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { Observable } from 'rxjs';
import { SectorDTO } from '../Models/sector.model';

@Injectable({
  providedIn: 'root'
})
export class SectorService {
  private baseUrl = 'http://localhost:8080/api/sectors';

  constructor(private http: HttpClient) {}

  getAllSectors(): Observable<SectorDTO[]> {
    return this.http.get<SectorDTO[]>(this.baseUrl);
  }

  getSectorById(id: number): Observable<SectorDTO> {
    return this.http.get<SectorDTO>(`${this.baseUrl}/${id}`);
  }

  createSector(sector: SectorDTO): Observable<SectorDTO> {
    return this.http.post<SectorDTO>(this.baseUrl, sector);
  }

  updateSector(id: number, sector: SectorDTO): Observable<SectorDTO> {
    return this.http.put<SectorDTO>(`${this.baseUrl}/${id}`, sector);
  }

  deleteSector(id: number): Observable<void> {
    return this.http.delete<void>(`${this.baseUrl}/${id}`);
  }

  uploadSectorImage(id: number, file: File): Observable<SectorDTO> {
    const form = new FormData();
    form.append('file', file, file.name);
    return this.http.post<SectorDTO>(`${this.baseUrl}/${id}/image`, form);
  }
}
