import { Injectable } from "@nestjs/common"
import { CreateCancelReasonDto } from "../dto/create-cancel-reason.dto"
import { UpdateCancelReasonDto } from "../dto/update-cancel-reason.dto"
import { CancelReason } from "../entities/cancel-reasons.entity"
import { failureResponse, successResponse } from "src/common/response/response"
import { code } from "src/common/response/response.code"
import { errorMessage, successMessage } from "src/utils/helpers"
import { messageKey } from "src/constants/message-keys"
import { CancelReasonRepository } from "../repositories/cancel-reasons-repository"

@Injectable()
export class CancelReasonsService {
  constructor(
    private readonly cancelReasonRepository: CancelReasonRepository,
  ) {}

  async create(createCancelReasonDto: CreateCancelReasonDto) {
    try {
      const createCancelReasonType = new CancelReason()
      Object.assign(createCancelReasonType, createCancelReasonDto)
      await this.cancelReasonRepository.save(createCancelReasonType)
      return successResponse(
        code.SUCCESS,
        successMessage(messageKey.data_add, { ":data": "Cancel reason" }),
      )
    } catch (error) {
      return failureResponse(
        code.ERROR,
        errorMessage(messageKey.something_went_wrong),
      )
    }
  }

  async findAll(type?: string) {
    try {
      const allowedTypes = ["admin", "driver", "customer"]

      const filters: any = {}

      if (type && allowedTypes.includes(type)) {
        filters.type = type
      }

      const result = await this.cancelReasonRepository.getByParams({
        where: filters,
        orderBy: { id: "ASC" },
      })

      return successResponse(
        code.SUCCESS,
        messageKey.cancel_reason_data_retrieved,
        result,
      )
    } catch (error) {
      return failureResponse(code.ERROR, messageKey.something_went_wrong)
    }
  }

  async update(id: number, updateCancelReasonDto: UpdateCancelReasonDto) {
    try {
      const cancelReason = (await this.cancelReasonRepository.getByParams({
        where: { id },
        findOne: true,
      })) as CancelReason
      if (cancelReason) {
        return failureResponse(
          code.DATA_NOT_FOUND,
          errorMessage(messageKey.data_not_found, { ":data": "Cancel reason" }),
        )
      }
      const updatedReason = await this.cancelReasonRepository.save(
        updateCancelReasonDto as any,
        { id },
      )
      return successResponse(
        code.SUCCESS,
        successMessage(messageKey.data_update, { ":data": "Cancel reason" }),
        updatedReason,
      )
    } catch (error) {
      return failureResponse(
        code.ERROR,
        errorMessage(messageKey.something_went_wrong),
      )
    }
  }

  async remove(id: number) {
    try {
      const result = await this.cancelReasonRepository.remove({ id })
      if (result && "affected" in result && result.affected === 0) {
        return failureResponse(
          code.DATA_NOT_FOUND,
          errorMessage(messageKey.data_not_found, { ":data": "Cancel reason" }),
        )
      }
      return successResponse(
        code.SUCCESS,
        successMessage(messageKey.data_removed, { ":data": "Cancel reason" }),
      )
    } catch (error) {
      return failureResponse(
        code.ERROR,
        errorMessage(messageKey.something_went_wrong),
      )
    }
  }
}
