class Api::V1::Projects::Pulls::Reviews::CreateService < ApplicationService
  include ActiveModel::Model

  attr_reader :project, :pull_request, :issue, :status, :commit_id, :content, :current_user
  attr_accessor :review, :journal

  validates :status, inclusion: { in: %w(common approved rejected), message: '请输入正确的Type'}

  def initialize(project, pull_request, params, current_user)
    @project = project 
    @pull_request = pull_request 
    @issue = pull_request&.issue
    @status = params[:status]
    @commit_id = params[:commit_id]
    @content = params[:content]
    @current_user = current_user
  end

  def call
    raise Error, errors.full_messages.join(", ") unless valid?
    ActiveRecord::Base.transaction do 
      create_review 
      # create_journal
    end 

    @review
  # rescue 
  #   raise Error, '服务器错误，请联系系统管理员！'
  end

  private 
  def create_review  
    @review = pull_request.reviews.create!(status: status, content: content, commit_id: commit_id, reviewer_id: @current_user.id)
  end


end