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'insegnante
export interface Teacher {
  id: number;
  first_name: string;
  last_name: string;
  email?: string | null;
  phone?: string | null;
  date_of_birth?: string | null; // Manteniamo stringa per semplicità, potremmo usare Date
  hire_date?: string | null;
  qualifications?: string | null;
  is_active: boolean; // O number se il backend restituisce 0/1
  created_at: string;
  updated_at: string;
}

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

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

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

  // GET /api/teachers
  getTeachers(): Observable<Teacher[]> {
    // Il backend restituisce già un array ridotto per la lista
    return this.get<Teacher[]>(`${this.controllerName}`);
  }

  // GET /api/teachers/{id}
  getTeacher(id: number): Observable<Teacher> {
    return this.get<Teacher>(`${this.controllerName}/${id}`);
  }

  // POST /api/teachers
  addTeacher(teacher: TeacherInput): Observable<Teacher> {
    return this.post<Teacher>(`${this.controllerName}`, teacher);
  }

  // PUT /api/teachers/{id}
  updateTeacher(id: number, teacher: TeacherInput): Observable<Teacher> {
    return this.put<Teacher>(`${this.controllerName}/${id}`, teacher);
  }

  // DELETE /api/teachers/{id}
  deleteTeacher(id: number): Observable<null> {
    return this.delete<null>(`${this.controllerName}/${id}`);
  }
}
