import { Injectable } from "@nestjs/common";
import { ScheduleRepo } from "./schedule.repo";
import * as DTO from './dto';

@Injectable()
export class ScheduleService {
  constructor(
    private readonly scheduleRepo: ScheduleRepo
  ) {}

  async getList(accountId: string, data: DTO.ListRequest): Promise<DTO.ListResponse> {
    const result = await this.scheduleRepo.getList(accountId, data);
    
    return {
      success: true,
      data: result
    };
  }

  async getDetail(id: string) {
    const result = await this.scheduleRepo.getDetail(id);

    if (result.length < 1) {
      return {
        success: false,
        message: '존재하지 않는 일정입니다.'
      };
    }

    const data = {
      ...result[0],
      startDate: new Date(result[0].startDate),
      endDate: new Date(result[0].endDate)
    }

    return {
      success: true,
      data: data
    };
  }

  async create(accountId: string, data: DTO.CreateRequest): Promise<DTO.CreateResponse> {
    const { name, content, startDate, endDate, startTime, endTime, style, type } = data;

    const result = await this.scheduleRepo.create(
      accountId,
      name,
      startDate,
      endDate,
      startTime,
      endTime,
      style,
      content,
      type
    );

    if (result.rowCount) {
      return {
        success: true,
        message: "일정이 생성되었습니다."
      };
    } else {
      return {
        success: false,
        message: "일정 생성에 실패하였습니다."
      }
    }
  }
}