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

// Interfaccia per i dati dell'anno scolastico
export interface SchoolYear {
  id: number;
  name: string;
  start_date: string; // Formato YYYY-MM-DD
  end_date: string;   // Formato YYYY-MM-DD
  is_active: boolean; // O number
  created_at: string;
  updated_at: string;
}

// Interfaccia per i dati parziali usati in creazione/modifica
export type SchoolYearInput = Partial<Omit<SchoolYear, 'id' | 'created_at' | 'updated_at'>>;

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

  //private apiUrl = 'http://localhost:8000/api/school-years';

  private httpOptions = new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' });

  constructor(authService: AuthService, http: HttpClient) {
    super(authService, http, 'school-years');
  }

  // GET /api/school-years
  getSchoolYears(): Observable<SchoolYear[]> {
    return this.get<SchoolYear[]>(`${this.controllerName}`);
  }

  // GET /api/school-years/{id}
  getSchoolYear(id: number): Observable<SchoolYear> {
    return this.get<SchoolYear>(`${this.controllerName}/${id}`);
  }

  // POST /api/school-years
  addSchoolYear(schoolYear: SchoolYearInput): Observable<SchoolYear> {
    return this.post<SchoolYear>(`${this.controllerName}`, schoolYear);
  }

  // PUT /api/school-years/{id}
  updateSchoolYear(id: number, schoolYear: SchoolYearInput): Observable<SchoolYear> {
    return this.put<SchoolYear>(`${this.controllerName}/${id}`, schoolYear);
  }

  // DELETE /api/school-years/{id}
  deleteSchoolYear(id: number): Observable<null> {
    return this.delete<null>(`${this.controllerName}/${id}`);
  }
}
