import {Injectable} from '@angular/core';
import {ApiService} from "../../shared/service/api.service";
import {Employee, EmployeePermission} from "../../shared/model/employee";
import {Observable} from "rxjs";

@Injectable({
  providedIn: 'root'
})
export class EmployeeService {
  constructor(
    private api: ApiService
  ) {
  }

  get all(): Observable<Employee[]> {
    return this.api.get<Employee[]>('/employee')
  }

  get(id: number): Observable<Employee> {
    return this.api.get<Employee>(`/employee/${id}`)
  }

  save(id: number, firstName: string, lastName: string, password: string, group: number, permissions: EmployeePermission[]): Observable<Employee> {
    const employee = {id, firstName, lastName, password, group, permissions}
    return this.api.post<Employee>('/employee', employee)
  }

  update(id: number, firstName: string, lastName: string, permissions: EmployeePermission[]): Observable<void> {
    const employee = {id, firstName, lastName, permissions}
    return this.api.put<void>('/employee', employee)
  }

  updatePassword(id: number, password: string): Observable<void> {
    return this.api.put<void>(`/employee/${id}/password`, password, true, {headers: {contentType: 'text/plain'}})
  }

  delete(id: number): Observable<void> {
    return this.api.delete<void>(`/employee/${id}`)
  }
}
