# == Schema Information
#
# Table name: pages
#
#  id                :integer          not null, primary key
#  user_id           :integer          not null
#  project_id        :integer          not null
#  identifier        :string(255)
#  site_name         :string(255)
#  language_frame    :integer          default("0")
#  theme             :string(255)
#  last_build_at     :datetime
#  state             :boolean          default("1")
#  state_description :string(255)
#  created_at        :datetime         not null
#  updated_at        :datetime         not null
#  last_build_info   :text(65535)
#  build_state       :boolean
#
# Indexes
#
#  index_pages_on_project_id  (project_id)
#  index_pages_on_user_id     (user_id)
#

class Page < ApplicationRecord 
  belongs_to :user
  belongs_to :project

  # language_frame 前端语言框架
  enum language_frame: { hugo: 0, jekyll: 1, hexo: 2, files: 3}
  
  after_create do
    PageService.genernate_user(user_id)
  end

  before_destroy do
    PageService.close_site(user_id, identifier)
  end

  before_save do
    if state_changed? && state == false
      PageService.close_site(user_id, identifier)
    end
  end

  def deploy_page(branch)
    PageService.deploy_page(branch,self.id)
  end

  def url 
    @deploy_domain = EduSetting.find_by_name("site_page_deploy_domain").try(:value)
    "http://#{identifier}"
  end

  def build_script_path
    "#{language_frame}_build"
  end

end
