import { AgreementConsent } from '@dvn/core/models/agreement-consent';
import { Membership } from '@dvn/core/models/membership';
import { notFound } from '@dvn/dvn-mock/api/utils';
import { STORAGE_KEY_KIMLIK_USER_ID } from '@dvn/kimlik/api/kimlik-api';
export class UyeApiDemo {
  constructor(database, storage, logger) {
    this.logger = logger.new('UyeApiDemo').setColor('river');
    this.database = database;
    this.storage = storage;
  }
  getAppConfigOfMembership(id) {
    this.logger.debugGroup('getAppConfigOfMembership', id);
    for (let i = 0; i < Object.keys(this.database.dvnAppConfigs).length; i++) {
      const key = Object.keys(this.database.dvnAppConfigs)[i];
      if (this.database.dvnAppConfigs[key].membership._id === id) {
        return Promise.resolve(this.database.dvnAppConfigs[key]);
      }
    }
    this.logger.error('getAppConfigOfMembership', 'not-found');
    return Promise.reject({ status: 404 });
  }
  addAuthInterceptor(_token) {
    this.logger.debug('addAuthInterceptor');
  }
  consentAgreement(agreementId) {
    const userId = this.storage.get(STORAGE_KEY_KIMLIK_USER_ID);
    const user = this.database.kimlikUsers[userId];
    if (!user) {
      this.logger.warnGroup('consentAgreement: user not found', { userId, agreementId });
      return Promise.reject(notFound());
    }
    const agreement = this.database.onayAgreements[agreementId];
    if (!agreement) {
      this.logger.warnGroup('consentAgreement: agreement not found', { userId, agreementId });
      return Promise.reject(notFound());
    }
    const totalConsents = Object.keys(this.database.onayConsents).length;
    const newConsentId = `onayConsents${totalConsents + 1}`;
    const consent = AgreementConsent.decode({
      _id: newConsentId,
      _created: new Date(),
      _updated: new Date(),
      agreement,
      user,
    });
    this.database.onayConsents[newConsentId] = consent;
    return Promise.resolve(consent);
  }
  getAgreement(id) {
    return Promise.resolve(this.database.onayAgreements[id]);
  }
  activateMembership(membershipConfigId, membershipId) {
    const membershipConfig = this.database.uyeMembershipConfigs[membershipConfigId];
    if (!membershipConfig) {
      this.logger.warnGroup('getMembership: config not found', { membershipConfigId, membershipId });
      return Promise.reject(`UyeApiDemo.getMembership: Membership config "${membershipConfigId}" not found`);
    }
    const membershipsTable = this.database[membershipConfig.name];
    if (!membershipsTable) {
      this.logger.warnGroup('getMembership: table not found', { membershipConfigId, membershipId });
      return Promise.reject(`UyeApiDemo.getMembership: Memberships table "${membershipsTable}" not found`);
    }
    const keys = Object.keys(membershipsTable);
    for (let i = 0; i < keys.length; i++) {
      const key = keys[i];
      if (membershipsTable[key]._id === membershipId) {
        this.logger.debugGroup('activateMembership', {
          membership: membershipsTable[key],
          membershipConfigId,
          membershipId,
        });
        membershipsTable[key].status = 'active';
        return Promise.resolve(membershipsTable[key]);
      }
    }
    this.logger.warnGroup('getMembership: membership not found', { membershipConfigId, membershipId });
    return Promise.reject(notFound());
  }
  getMembership(membershipConfigId, membershipId) {
    this.logger.debug('getMembership');
    const membershipConfig = this.database.uyeMembershipConfigs[membershipConfigId];
    if (!membershipConfig) {
      this.logger.warnGroup('getMembership: config not found', { membershipConfigId, membershipId });
      return Promise.reject(`UyeApiDemo.getMembership: Membership config "${membershipConfigId}" not found`);
    }
    const membershipsTable = this.database[membershipConfig.name];
    if (!membershipsTable) {
      this.logger.warnGroup('getMembership: table not found', { membershipConfigId, membershipId });
      return Promise.reject(`UyeApiDemo.getMembership: Memberships table "${membershipsTable}" not found`);
    }
    const keys = Object.keys(membershipsTable);
    for (let i = 0; i < keys.length; i++) {
      const key = keys[i];
      if (membershipsTable[key]._id === membershipId) {
        this.logger.debugGroup('getMembership', {
          membership: membershipsTable[key],
          membershipConfigId,
          membershipId,
        });
        return Promise.resolve(Membership.decode(membershipsTable[key]));
      }
    }
    this.logger.warnGroup('getMembership: membership not found', { membershipConfigId, membershipId });
    return Promise.reject(notFound());
  }
  getMembershipsOfUser(userId) {
    this.logger.debugGroup('getMembershipsOfUser', { userId });
    const ms = [];
    for (let membershipConfigId in this.database.uyeMembershipConfigs) {
      const membershipsTable = this.database[this.database.uyeMembershipConfigs[membershipConfigId].name];
      if (!membershipsTable) {
        return Promise.reject(`UyeApiDemo.getMembership: Memberships table "${membershipsTable}" not found`);
      }
      for (let membershipId in membershipsTable) {
        if (membershipsTable[membershipId].user._id === userId) {
          ms.push(membershipsTable[membershipId]);
        }
      }
    }
    return Promise.resolve({ results: ms });
  }
  getMembershipConfig(membershipConfigId) {
    const membershipConfig = this.database[membershipConfigId];
    if (!membershipConfig) {
      return Promise.reject(`UyeApiDemo.getMembership: Membership config "${membershipConfigId}" not found`);
    }
    return Promise.resolve(membershipConfig);
  }
  getMembershipConfigWithName(name) {
    const membershipConfigIds = Object.keys(this.database.uyeMembershipConfigs);
    for (let i = 0; i < membershipConfigIds.length; i++) {
      const membershipConfigId = membershipConfigIds[i];
      const membershipConfig = this.database.uyeMembershipConfigs[membershipConfigId];
      if (membershipConfig.name === name) {
        this.logger.debugGroup('getMembershipConfigWithName', { name, membershipConfig });
        return Promise.resolve(membershipConfig);
      }
    }
    this.logger.errorGroup('getMembershipConfigWithName: membership not found', { name });
    return Promise.reject(notFound());
  }
  updateMembership(membershipConfigId, membershipId, data) {
    this.logger.debug('updateMembership');
    const membershipConfig = this.database.uyeMembershipConfigs[membershipConfigId];
    if (!membershipConfig) {
      this.logger.warnGroup('updateMembership: config not found', { membershipConfigId, membershipId });
      return Promise.reject(notFound());
    }
    const membershipsTable = this.database[membershipConfig.name];
    if (!membershipsTable) {
      this.logger.warnGroup('updateMembership: table not found', { membershipConfigId, membershipId });
      return Promise.reject(notFound());
    }
    const keys = Object.keys(membershipsTable);
    for (let i = 0; i < keys.length; i++) {
      const key = keys[i];
      if (membershipsTable[key]._id === membershipId) {
        const membership = membershipsTable[key];
        this.logger.debugGroup('updateMembership', {
          membership,
          membershipConfigId,
          membershipId,
        });
        membershipsTable[key] = Membership.decode(Object.assign(Object.assign({}, membership), data));
        return Promise.resolve(membershipsTable[key]);
      }
    }
    this.logger.warnGroup('getMembership: membership not found', { membershipConfigId, membershipId });
    return Promise.reject(notFound());
  }
}
