import { HttpErrorResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { CreateAccountDto, EditAccountDto } from '@api/models';
import {
  AccountsApiClient,
  RolesApiClient,
  StipulatoriApiClient,
} from '@api/services';
import { catchError, throwError } from 'rxjs';

export type PASSWORD_MATCH_TYPE = 'MIN_8' | 'a-z' | 'A-Z';

@Injectable({
  providedIn: 'root',
})
export class UtentiService {
  constructor(
    private accountsApiClient: AccountsApiClient,
    private rolesApiClient: RolesApiClient,
    private stipulatoriApiClient: StipulatoriApiClient,
  ) {}

  getUsers() {
    const params = {
      body: {},
    };
    return this.accountsApiClient.accountsControllerFetchAccounts(params).pipe(
      catchError((err: HttpErrorResponse) => {
        return throwError(() => err);
      }),
    );
  }

  getRoles() {
    const params = {
      body: {},
    };
    return this.rolesApiClient.rolesControllerFetchRoles(params).pipe(
      catchError((err: HttpErrorResponse) => {
        return throwError(() => err);
      }),
    );
  }

  getStipulatori() {
    return this.stipulatoriApiClient
      .stipulatoriControllerFindManyStipulatori()
      .pipe(
        catchError((err: HttpErrorResponse) => {
          return throwError(() => err);
        }),
      );
  }

  getStipulatoriByAccountId(idAccount: number) {
    const params = {
      id: idAccount,
    };

    return this.accountsApiClient
      .accountsControllerGetAccountStipulatori(params)
      .pipe(
        catchError((err: HttpErrorResponse) => {
          return throwError(() => err);
        }),
      );
  }

  editStipulatori(id: number, params: any) {
    const param = {
      id: id,
      body: params,
    };
    return this.accountsApiClient
      .accountsControllerEditAccountStipulatori(param)
      .pipe(
        catchError((err: unknown) => {
          return throwError(() => err);
        }),
      );
  }

  getUserById(id: number) {
    const params = {
      id: id,
    };
    return this.accountsApiClient.accountsControllerFetchAccount(params).pipe(
      catchError((err: HttpErrorResponse) => {
        return throwError(() => err);
      }),
    );
  }

  createUser(params: CreateAccountDto) {
    const param = {
      body: params,
    };
    return this.accountsApiClient.accountsControllerCreateAccount(param).pipe(
      catchError((err: HttpErrorResponse) => {
        return throwError(() => err);
      }),
    );
  }

  editUser(id: number, params: EditAccountDto) {
    const param = {
      id: id,
      body: params,
    };
    return this.accountsApiClient.accountsControllerEditAccount(param).pipe(
      catchError((err: HttpErrorResponse) => {
        return throwError(() => err);
      }),
    );
  }

  deleteUser(id: number) {
    const param = {
      id: id,
    };
    return this.accountsApiClient.accountsControllerDeleteAccount(param).pipe(
      catchError((err: HttpErrorResponse) => {
        return throwError(() => err);
      }),
    );
  }
  sendActivationMail(id: number) {
    const param = {
      id: id,
    };
    return this.accountsApiClient
      .accountsControllerSendActivationLink(param)
      .pipe(
        catchError((err: HttpErrorResponse) => {
          return throwError(() => err);
        }),
      );
  }
}
