import { ClassError, ObjectBase } from '@tomei/general';
import { IHirerChangeRequestNewHirerAttr } from '../../interfaces/hirer-change-request-new-hirer-attr.interface';
import { HirerChangeRequestNewHirerRepository } from './hirer-change-request-new-hirer.repository';
import { LoginUser } from '@tomei/sso';
import { ActionEnum, Activity } from '@tomei/activity-history';

export class HirerChangeRequestNewHirer
  extends ObjectBase
  implements IHirerChangeRequestNewHirerAttr
{
  ObjectId: string;
  ObjectName: string;
  ObjectType: string = 'HirerChangeRequestNewHirer';
  TableName: string = 'hirerChangeRequest_NewHirer';

  RequestId: string;
  FullName: string;
  Email: string;
  IdType: string;
  IdNo: string;
  ContactNo: string;
  Relationship: string;
  Address: string;
  City: string;
  State: string;
  Postcode: string;
  Country: string;

  get NewHirerId(): string {
    return this.ObjectId;
  }

  set NewHirerId(value: string) {
    this.ObjectId = value;
  }

  protected static _Repository = new HirerChangeRequestNewHirerRepository();

  protected constructor(attr?: IHirerChangeRequestNewHirerAttr) {
    super();
    if (attr) {
      this.NewHirerId = attr.NewHirerId;
      this.RequestId = attr.RequestId;
      this.FullName = attr.FullName;
      this.Email = attr.Email;
      this.IdType = attr.IdType;
      this.IdNo = attr.IdNo;
      this.ContactNo = attr.ContactNo;
      this.Relationship = attr.Relationship;
      this.Address = attr.Address;
      this.City = attr.City;
      this.State = attr.State;
      this.Postcode = attr.Postcode;
      this.Country = attr.Country;
    }
  }

  public static async init(newHirerId?: string, dbTransaction?: any) {
    try {
      if (newHirerId) {
        const hirerChangeReqNewHirer =
          await HirerChangeRequestNewHirer._Repository.findByPk(
            newHirerId,
            dbTransaction,
          );
        if (hirerChangeReqNewHirer) {
          return new HirerChangeRequestNewHirer(
            hirerChangeReqNewHirer.get({ plain: true }),
          );
        } else {
          throw new ClassError(
            'HirerChangeRequestNewHirer',
            'HirerChangeRequestNewHirerErrMsg00',
            'HirerChangeRequestNewHirer not found',
          );
        }
      }
      return new HirerChangeRequestNewHirer();
    } catch (error) {
      throw error;
    }
  }

  public static async findAll(
    search: {
      RequestId?: string;
    },
    dbTransaction?: any,
  ): Promise<HirerChangeRequestNewHirer[]> {
    try {
      const results = await HirerChangeRequestNewHirer._Repository.findAll({
        where: {
          ...search,
        },
        transaction: dbTransaction,
      });
      return results.map(
        (result) => new HirerChangeRequestNewHirer(result.get({ plain: true })),
      );
    } catch (error) {
      console.error('Error finding all HirerChangeRequestNewHirer:', error);
      throw error;
    }
  }

  toJSON(): IHirerChangeRequestNewHirerAttr {
    return {
      NewHirerId: this.NewHirerId,
      RequestId: this.RequestId,
      FullName: this.FullName,
      Email: this.Email,
      IdType: this.IdType,
      IdNo: this.IdNo,
      ContactNo: this.ContactNo,
      Relationship: this.Relationship,
      Address: this.Address,
      City: this.City,
      State: this.State,
      Postcode: this.Postcode,
      Country: this.Country,
    };
  }

  public async create(loginUser: LoginUser, dbTransaction?: any) {
    //This method will create a new Hirer Change Request New Hirer.
    try {
      //Insert record into the database
      this.ObjectId = this.createId();
      const entityValueAfter = this.toJSON();
      const newHirer = await HirerChangeRequestNewHirer._Repository.create(
        entityValueAfter,
        {
          transaction: dbTransaction,
        },
      );

      //Create activity log
      const activity = new Activity();
      activity.ObjectId = this._createId();
      activity.Action = ActionEnum.CREATE;
      activity.Description = `Created new Hirer Change Request New Hirer`;
      activity.EntityId = newHirer.NewHirerId;
      activity.EntityType = this.ObjectType;
      activity.EntityValueBefore = JSON.stringify({});
      activity.EntityValueAfter = JSON.stringify(newHirer);

      await activity.create(loginUser.ObjectId, dbTransaction);
    } catch (error) {
      console.error('Error creating HirerChangeRequestNewHirer:', error);
      throw error;
    }
  }
}
