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 del contratto (inclusi campi joinati)
export interface TeacherContract {
  id: number;
  teacher_id: number;
  school_year_id: number;
  structure_id?: number | null; // Opzionale
  contract_type?: string | null;
  start_date: string; // YYYY-MM-DD
  end_date?: string | null;   // YYYY-MM-DD
  weekly_hours?: number | null;
  salary?: number | null;
  notes?: string | null;
  created_at: string;
  updated_at: string;
  // Campi joinati (aggiunti dal backend)
  teacher_first_name?: string;
  teacher_last_name?: string;
  school_year_name?: string;
  structure_name?: string | null;
}

// Interfaccia per i dati di input (senza ID e campi readonly/joinati)
export type TeacherContractInput = Partial<Omit<TeacherContract,
  'id' | 'created_at' | 'updated_at' |
  'teacher_first_name' | 'teacher_last_name' | 'school_year_name' | 'structure_name'
>>;

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

  // Non più necessario con application/json (è il default di HttpClient per POST/PUT con oggetti)
  // private httpOptions = {
  //   headers: new HttpHeaders({ 'Content-Type': 'application/x-www-form-urlencoded' })
  // };

  constructor(authService: AuthService, http: HttpClient) {
    super(authService, http, 'teacher-contracts');
  }

  // GET /api/teacher-contracts (con filtri opzionali)
  getTeacherContracts(filters: { teacher_id?: number, school_year_id?: number, structure_id?: number } = {}): Observable<TeacherContract[]> {
    let params = new HttpParams();
    if (filters.teacher_id) {
      params = params.set('teacher_id', filters.teacher_id.toString());
    }
    if (filters.school_year_id) {
      params = params.set('school_year_id', filters.school_year_id.toString());
    }
     if (filters.structure_id) {
      params = params.set('structure_id', filters.structure_id.toString());
    }
    return this.get<TeacherContract[]>(`${this.controllerName}`, params);
  }

  // GET /api/teacher-contracts/{id}
  getTeacherContract(id: number): Observable<TeacherContract> {
    return this.get<TeacherContract>(`${this.controllerName}/${id}`);
  }

  // POST /api/teacher-contracts
  addTeacherContract(contract: TeacherContractInput): Observable<TeacherContract> {
    // Invia l'oggetto direttamente, HttpClient lo serializzerà come JSON
    return this.post<TeacherContract>(`${this.controllerName}`, contract); // Rimosso .toString() e httpOptions
  }

  // PUT /api/teacher-contracts/{id}
  updateTeacherContract(id: number, contract: TeacherContractInput): Observable<TeacherContract> {
    // Invia l'oggetto direttamente
    return this.put<TeacherContract>(`${this.controllerName}/${id}`, contract); // Rimosso .toString() e httpOptions
  }

  // DELETE /api/teacher-contracts/{id}
  deleteTeacherContract(id: number): Observable<null> {
    return this.delete<null>(`${this.controllerName}/${id}`);
  }
}
