import { Injectable } from '@angular/core';
import { environments } from '../../../../environments/environments';
import { HttpClient, HttpHeaders } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class TareasService {
  private URL: string = environments.baseUrl;

  constructor(private http: HttpClient) { }

  private getHeaders(): HttpHeaders {
    const token = localStorage.getItem('token') || '';
    return new HttpHeaders({
      'Content-Type': 'application/json',
      'Authorization': `Bearer ${token}`
    });
  }

  getTareas() {
    return this.http.get(`${this.URL}/getAllTareas`, { headers: this.getHeaders() });
  }

  getByFechaEntrega(fechaEntrega: string) {
    return this.http.get(`${this.URL}/tareas/fecha-entrega/${fechaEntrega}`, { headers: this.getHeaders() });
  }

  getByFechaAsignacion(fechaAsignacion: string) {
    return this.http.get(`${this.URL}/tareas/fecha-asignacion/${fechaAsignacion}`, { headers: this.getHeaders() });
  }

  getByMateria(idMateria: string) {
    return this.http.get(`${this.URL}/tareas/materia/${idMateria}`, { headers: this.getHeaders() });
  }

  getMaterias30Dias() {
    return this.http.get(`${this.URL}/tareas/materiaUlt30/`, { headers: this.getHeaders() });
  }

  getTipoTareas() {
    return this.http.get(`${this.URL}/tipoTarea`, { headers: this.getHeaders() });
  }

  crearTarea(tarea: any) {
    return this.http.post(`${this.URL}/crearTarea`, tarea, { headers: this.getHeaders() });
  }

  actualizarTarea(idTarea: any, tarea: any) {
    return this.http.put(`${this.URL}/tareas/${idTarea}`, tarea, { headers: this.getHeaders() });
  }

  getAlumnos(idMateria: string) {
    return this.http.get(`${this.URL}/materias/${idMateria}/alumnos`, { headers: this.getHeaders() });
  }

  asignarTarea(usuarioTarea: any) {
    return this.http.post(`${this.URL}/tareas-usuarios`, usuarioTarea, { headers: this.getHeaders() });
  }

  eliminarTarea(id: string) {
    
    return this.http.put(`${this.URL}/tareaDelete/${id}`, null, { headers: this.getHeaders() });
  }

  habilitarTarea(id: string) {
    return this.http.put(`${this.URL}/tareaEnable/${id}`, null, { headers: this.getHeaders() });
  }

}
