import { DataModel, transformArrayDataModel } from '@imengyu/js-request-transform';
import { AppServerRequestModule } from '../RequestModules';
import { transformSomeToArray } from '../Utils';
import { VillageListItem } from './LightVillageApi';

export class FollowedUserListItem extends DataModel<FollowedUserListItem> {
  constructor() {
    super(FollowedUserListItem, '关注用户');
    this.setNameMapperCase('Camel', 'Snake');
    this._convertTable = {
      id: { clientSide: 'number', serverSide: 'number', clientSideRequired: true },
      userId: { clientSide: 'number', serverSide: 'number' },
    };
    this._convertKeyType = (key) => {
      if (key.endsWith('timeText')) return undefined;
      if (key.endsWith('time')) {
        return {
          clientSide: 'date',
          serverSide: 'number',
        };
      }
      return undefined;
    };
  }

  id!: number;
  nickname = '';
  username = '';
  avatar = '';
  mobile = '';
  gender = 0 as number | null;
  birthday = null as string | null;
  userId = 0 as number | null;
  followtime = '' as string | null;
  prevtime = null as Date | null;
  logintime = null as Date | null;
  jointime = null as Date | null;
  prevtimeText = '' as string | null;
  logintimeText = '' as string | null;
  jointimeText = '' as string | null;
}

export class FollowVillageApi extends AppServerRequestModule<DataModel> {
  constructor() {
    super();
  }

  async followVillage(villageId: number) {
    const res = await this.post<null>('/village/village/follow', '关注村社', {
      village_id: villageId,
    });
    return res.requireData();
  }

  async unfollowVillage(villageId: number) {
    const res = await this.post<null>('/village/village/unfollow', '取消关注村社', {
      village_id: villageId,
    });
    return res.requireData();
  }

  async getFollowVillageList(options?: {
    page?: number;
    pageSize?: number;
    keywords?: string;
    region?: number;
    userId?: number;
  }) {
    const res = await this.post<{
      total: number;
      per_page?: number;
      current_page?: number;
      last_page?: number;
      data: any[];
    }>('/village/village/followVillage', '关注村社列表', {
      page: options?.page,
      pageSize: options?.pageSize,
      keywords: options?.keywords,
      region: options?.region,
      user_id: options?.userId,
    });

    const data = res.requireData();
    return {
      total: data.total ?? 0,
      list: transformArrayDataModel<VillageListItem>(
        VillageListItem,
        transformSomeToArray(data.data),
        '关注村社',
        true,
      ),
    };
  }

  async getFollowedUsers(options: {
    villageId: number;
    page?: number;
    pageSize?: number;
    keywords?: string;
    region?: number;
  }) {
    const res = await this.post<{
      total: number;
      per_page?: number;
      current_page?: number;
      last_page?: number;
      data: any[];
    }>('/village/village/followedUsers', '关注用户列表', {
      village_id: options.villageId,
      page: options.page,
      pageSize: options.pageSize,
      keywords: options.keywords,
      region: options.region,
    });

    const data = res.requireData();
    return {
      total: data.total ?? 0,
      list: transformArrayDataModel<FollowedUserListItem>(
        FollowedUserListItem,
        transformSomeToArray(data.data),
        '关注用户',
        true,
      ),
    };
  }
}

export default new FollowVillageApi();
