import { Injectable, inject } from '@angular/core';
import { Actions, createEffect, ofType } from '@ngrx/effects';
import { Store } from '@ngrx/store';
import { of } from 'rxjs';
import { map, exhaustMap, catchError, withLatestFrom, filter } from 'rxjs/operators';
import { HrEmployeeService } from '../../../services/hr-employee.service';
import { HrEmployeesActions } from './employees.actions';
import { selectHrEmployeesLoaded } from './employees.reducer';

@Injectable()
export class HrEmployeesEffects {
  private actions$ = inject(Actions);
  private store = inject(Store);
  private service = inject(HrEmployeeService);

  loadHrEmployees$ = createEffect(() =>
    this.actions$.pipe(
      ofType(HrEmployeesActions.loadHREmployees),
      withLatestFrom(this.store.select(selectHrEmployeesLoaded)),
      filter(([action, loaded]) => !loaded || !!action.forceReload),
      exhaustMap(() =>
        this.service.listAll().pipe(
          map((response) =>
            HrEmployeesActions.loadHREmployeesSuccess({
              employees: response.data,
              meta: response.meta,
            })
          ),
          catchError((error) =>
            of(
              HrEmployeesActions.loadHREmployeesFailure({
                error: error.error?.message || 'Failed to load employees',
              })
            )
          )
        )
      )
    )
  );

  createHrEmployee$ = createEffect(() =>
    this.actions$.pipe(
      ofType(HrEmployeesActions.createHREmployee),
      exhaustMap(({ employee }) =>
        this.service.create(employee).pipe(
          map((response) =>
            HrEmployeesActions.createHREmployeeSuccess({ employee: response.data })
          ),
          catchError((error) =>
            of(
              HrEmployeesActions.createHREmployeeFailure({
                error: error.error?.message || 'Failed to create employee',
              })
            )
          )
        )
      )
    )
  );

  updateHrEmployee$ = createEffect(() =>
    this.actions$.pipe(
      ofType(HrEmployeesActions.updateHREmployee),
      exhaustMap(({ id, employee }) =>
        this.service.update(id, employee).pipe(
          map((response) =>
            HrEmployeesActions.updateHREmployeeSuccess({ employee: response.data })
          ),
          catchError((error) =>
            of(
              HrEmployeesActions.updateHREmployeeFailure({
                error: error.error?.message || 'Failed to update employee',
              })
            )
          )
        )
      )
    )
  );

  deleteHrEmployee$ = createEffect(() =>
    this.actions$.pipe(
      ofType(HrEmployeesActions.deleteHREmployee),
      exhaustMap(({ id }) =>
        this.service.delete(id).pipe(
          map(() => HrEmployeesActions.deleteHREmployeeSuccess({ id })),
          catchError((error) =>
            of(
              HrEmployeesActions.deleteHREmployeeFailure({
                error: error.error?.message || 'Failed to delete employee',
              })
            )
          )
        )
      )
    )
  );
}
