import { Injectable } from '@nestjs/common';
import {
  EventTopics,
  columnUniques,
  validateRelations,
} from 'src/core/strings/constants/interface.constants';
import { BaseCreateManager } from 'src/core/modules/domain/usecase/managers/base-create.manager';
import { TenantCreatedEvent } from '../../entities/event/tenant-created.event';
import { UserEntity } from 'src/modules/user-related/user/domain/entities/user.entity';
import { UserModel } from 'src/modules/user-related/user/data/models/user.model';
import { UserRole } from 'src/modules/user-related/user/constants';
import { hashPassword } from 'src/core/helpers/password/bcrypt.helpers';
import { SALT_OR_ROUNDS } from 'src/core/strings/constants/base.constants';

@Injectable()
export class CreateTenantManager extends BaseCreateManager<UserEntity> {
  get validateRelations(): validateRelations[] {
    return [];
  }

  async beforeProcess(): Promise<void> {
    Object.assign(this.data, {
      role: UserRole.TENANT,
      password: await hashPassword(this.data.password, SALT_OR_ROUNDS),
    });
    return;
  }

  async afterProcess(): Promise<void> {
    return;
  }

  async generateConfig(): Promise<void> {
    // TODO: Implement logic here
  }

  get uniqueColumns(): columnUniques[] {
    return [
      // validate username (all data)
      { column: 'username' },

      // validate name (hanya data dengan role tenant)
      {
        column: 'name',
        query: {
          role: UserRole.TENANT,
        },
      },
    ];
  }

  get eventTopics(): EventTopics[] {
    return [
      {
        topic: TenantCreatedEvent,
        data: this.data,
      },
    ];
  }

  get entityTarget(): any {
    return UserModel;
  }
}
