import { Injectable, Logger } from '@nestjs/common';
import { DataSource } from 'typeorm';
import { SortCriteria } from './entity/sort_criteria.entity';
import {
  TaskListSortableAttribute,
  SortDirection,
} from '../../common/types/sort';

@Injectable()
export class SortCriteriaRepositoryService {
  constructor(private dataSource: DataSource) {}
  private readonly logger = new Logger(SortCriteriaRepositoryService.name);
  /**
   * Updates sort criteria
   * @param userId
   * @param parameter
   * @param direction
   * @returns sort criteria
   */
  async updateSortCriteria(
    userId: number,
    parameter: TaskListSortableAttribute,
    direction: SortDirection,
  ): Promise<SortCriteria> {
    this.logger.log(
      ` ${this.updateSortCriteria.name}; parameter:${parameter}, direction:${direction}`,
    );
    return await this.dataSource.transaction(async (entityManager) => {
      const repo = entityManager.getRepository(SortCriteria);
      const targetSortCriteria = await repo.findOne({
        where: {
          user_id: userId,
        },
      });
      // 運用上はあり得ないが、プログラム上発生しうるのでエラーとして処理
      if (!targetSortCriteria) {
        throw new Error('sort criteria not found ');
      }

      targetSortCriteria.parameter = parameter;
      targetSortCriteria.direction = direction;

      const persisted = await repo.save(targetSortCriteria);
      return persisted;
    });
  }
  /**
   * Gets sort criteria
   * @param userId
   * @returns sort criteria
   */
  async getSortCriteria(userId: number): Promise<SortCriteria> {
    this.logger.log(` ${this.updateSortCriteria.name}; userId:${userId}`);

    const repo = this.dataSource.getRepository(SortCriteria);
    const sortCriteria = await repo.findOne({
      where: {
        user_id: userId,
      },
    });
    // 運用上はあり得ないが、プログラム上発生しうるのでエラーとして処理
    if (!sortCriteria) {
      throw new Error('sort criteria not found ');
    }

    return sortCriteria;
  }
}
