import axios from 'axios';

import { type IUtenteApp } from '@/shared/model/utente-app.model';

const baseApiUrl = 'api/utente-apps';

export default class UtenteAppService {
  find(id: number): Promise<IUtenteApp> {
    return new Promise<IUtenteApp>((resolve, reject) => {
      axios
        .get(`${baseApiUrl}/${id}`)
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  }

  getCurrentUser(): Promise<IUtenteApp> {
    return new Promise<IUtenteApp>((resolve, reject) => {
      axios
        .get(`${baseApiUrl}/current`)
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  }

  retrieve(): Promise<any> {
    return new Promise<any>((resolve, reject) => {
      axios
        .get(baseApiUrl)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  }

  delete(id: number): Promise<any> {
    return new Promise<any>((resolve, reject) => {
      axios
        .delete(`${baseApiUrl}/${id}`)
        .then(res => {
          resolve(res);
        })
        .catch(err => {
          reject(err);
        });
    });
  }

  create(entity: IUtenteApp): Promise<IUtenteApp> {
    return new Promise<IUtenteApp>((resolve, reject) => {
      axios
        .post(baseApiUrl, entity)
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  }

  update(entity: IUtenteApp): Promise<IUtenteApp> {
    return new Promise<IUtenteApp>((resolve, reject) => {
      axios
        .put(`${baseApiUrl}/${entity.id}`, entity)
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  }

  partialUpdate(entity: IUtenteApp): Promise<IUtenteApp> {
    return new Promise<IUtenteApp>((resolve, reject) => {
      axios
        .patch(`${baseApiUrl}/${entity.id}`, entity)
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  }

  saveCurrentUserProfile(entity: IUtenteApp): Promise<IUtenteApp> {
    return new Promise<IUtenteApp>((resolve, reject) => {
      axios
        .post(`${baseApiUrl}/current`, entity)
        .then(res => {
          resolve(res.data);
        })
        .catch(err => {
          reject(err);
        });
    });
  }
}
