import { BadRequestException, Inject, Injectable } from '@nestjs/common';
import { TaskActivityType } from './task_catalog';
import { TaskBoardPayload, TaskClaimResult, TaskProgressStore } from './tasks.types';

const CLIENT_ACTIVITY_TYPES: TaskActivityType[] = [
  'guide_opened',
  'notice_viewed',
  'map_visited',
  'course_board_opened',
  'facility_interacted',
];

@Injectable()
export class TaskService {
  constructor(@Inject('ITaskProgressStore') private readonly taskProgressStore: TaskProgressStore) {}

  async getBoard(userId: bigint): Promise<TaskBoardPayload> {
    return await this.taskProgressStore.getBoard(userId);
  }

  async recordClientActivity(userId: bigint, activity: TaskActivityType, targetId?: string): Promise<TaskBoardPayload> {
    if (!CLIENT_ACTIVITY_TYPES.includes(activity)) {
      throw new BadRequestException('该任务活动只能由服务器业务记录');
    }
    return await this.taskProgressStore.recordActivity(userId, activity, targetId?.trim());
  }

  async recordActivity(userId: bigint, activity: TaskActivityType, targetId?: string): Promise<TaskBoardPayload> {
    return await this.taskProgressStore.recordActivity(userId, activity, targetId?.trim());
  }

  async claim(userId: bigint, taskId: string): Promise<TaskClaimResult> {
    return await this.taskProgressStore.claim(userId, taskId.trim());
  }
}
