import { DomainEntity, UniqueID } from "@/core/common/domain";
import { Result } from "@repo/rdx-utils";

export interface ITabContextProps {
  tabId: UniqueID;
  userId: UniqueID;
}

export interface ITabContextPrimitives {
  id: string;
  tab_id: string;
  user_id: string;
}

export interface ITabContext {
  tabId: UniqueID;
  userId: UniqueID;
}

export class TabContext extends DomainEntity<ITabContextProps> implements ITabContext {
  static create(props: ITabContextProps, id?: UniqueID): Result<TabContext, Error> {
    return Result.ok(new this(props, id));
  }

  static createFromPrimitives(values: ITabContextPrimitives): Result<TabContext, Error> {
    const { user_id, tab_id } = values;
    const userIdOrError = UniqueID.create(user_id, false);
    const tabIdOrError = UniqueID.create(tab_id, false);

    const result = Result.combine([userIdOrError, tabIdOrError]);

    if (result.isFailure) {
      return Result.fail(result.error);
    }

    return TabContext.create({
      userId: userIdOrError.data,
      tabId: tabIdOrError.data,
    });
  }

  get tabId(): UniqueID {
    return this.props.tabId;
  }

  get userId(): UniqueID {
    return this.props.userId;
  }
}
