import { DataModel, transformDataModel, type KeyValue } from '@imengyu/js-request-transform';
import { AppServerRequestModule } from '../RequestModules';
import { LoginResult } from './UserApi';

export class UserInfo extends DataModel<UserInfo> {
  constructor() {
    super(UserInfo, "用户信息");
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
    }
  }

  id = 0;
  userId = 0;
  mobile = '';
  nickname = '';
  avatar = '';
  username = '';
  regionId = 0;
  isReviewer = false;
}

export class CollectUserApi extends AppServerRequestModule<DataModel> {

  constructor() {
    super();
  }
  async login(data: {
    mobile: string,
    password: string,
  }) {
    const res = await this.post<KeyValue>('/ich/inheritor/login', '登录', data);
    return transformDataModel<LoginResult>(LoginResult, {
      mainBodyUserInfo: {
        ...res.data,
        main_body_id: 1,
      },
      auth: {
        ...res.data,
      },
    });
  }
  async loginAdmin(data: {
    account: string,
    password: string,
  }) {
    return (await this.post('/user/adminLogin', '登录', {
      account: data?.account,
      password: data?.password,
    }, undefined, LoginResult)).data as LoginResult;
  }
}

export default new CollectUserApi();
