import { Inject, Injectable } from '@nestjs/common';
import { UserProvider } from 'src/core/sessions';
import { BLANK_USER } from 'src/core/strings/constants/base.constants';

@Injectable()
export class BaseReportService<PayloadEntity = any> {
  @Inject()
  protected userProvider: UserProvider;

  getUser() {
    try {
      return this.userProvider?.user;
    } catch (error) {
      return BLANK_USER;
    }
  }

  injectDefaultColumnCreate(payload: PayloadEntity): any {
    const currentDate = new Date().getTime();
    const user = this.getUser();
    return {
      ...payload,
      creator_id: user.id,
      creator_name: user.name,
      editor_id: user.id,
      editor_name: user.name,
      created_at: currentDate,
      updated_at: currentDate,
    };
  }

  injectDefaultColumnUpdate(payload: PayloadEntity) {
    const user = this.getUser();
    return {
      ...payload,
      editor_id: user.id,
      editor_name: user.name,
      updated_at: new Date().getTime(),
    };
  }
}
