# #Company class used for create company for add users to work
class CompaniesController < ApplicationController
  before_action :set_company, only: %i[show edit update destroy]
  before_action :authenticate_user!, except: %i[accept_invitation_to_company]
  # GET /companies
  # GET /companies.json
  def index
    @companies = Company.all
  end

  # GET /companies/1
  # GET /companies/1.json
  def show; end

  # GET /companies/new
  def new
    @company = Company.new
  end

  # GET /companies/1/edit
  def edit; end

  # POST /companies
  # POST /companies.json
  def create
    @company = Company.new(company_params.merge({user_id:current_user.id} ) )

    respond_to do |format|
      if @company.save
        current_user.company_id = @company.id
        current_user.incorporated_at = DateTime.now
        @user = current_user
        if @user.save!
          format.html { redirect_to @company, notice: 'Company was successfully created.' }
          format.json { render :show, status: :created, location: @company }
        else
          format.html { render :new }
          format.json { render json: @company.errors, status: :unprocessable_entity }
        end
      else
        format.html { render :new }
        format.json { render json: @company.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /companies/1
  # PATCH/PUT /companies/1.json
  def update
    respond_to do |format|
      if @company.update(company_params)
        format.html { redirect_to @company, notice: 'Company was successfully updated.' }
        format.json { render :show, status: :ok, location: @company }
      else
        format.html { render :edit }
        format.json { render json: @company.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /companies/1
  # DELETE /companies/1.json
  def destroy
    @company.destroy
    respond_to do |format|
      format.html { redirect_to @company, notice: 'Company was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  def send_email
    company_id = params[:company_id]
    email = params[:company][:email]
    pass = "password"
    @company = Company.find_by(id: company_id)
    @user = User.new(name: "temporary", lastname: "temporary", email: email, password: pass, company_id: company_id, incorporated_at: DateTime.now)
    if @user.save!
      respond_to do |format|
        UserMailer.with( company: @company, user: @user).welcome_to_company.deliver_later
        format.html { redirect_to @company, company: @company  }
        format.json { render :show, status: :created, location: company_subscribe_user_path }
      end
    else
      format.html { redirect_to  }
      format.json { render json: @company.errors, status: :unprocessable_entity }
    end
  end

  def subscribe_user
    company_id = params[:company_id]
    @company = Company.find_by(id: company_id)
    respond_to do |format|
        format.html { render :_addUsers, company: @company  }
        format.json { render :show, status: :created, location: company_subscribe_user_path }
    end
  end

  def accept_invitation_to_company
    redirect_to new_user_session_path
  end

  private

  # Use callbacks to share common setup or constraints between actions.
  def set_company
    @company = Company.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def company_params
    params.require(:company).permit(:id, :name, :description, :company, :email, :user_id)
  end
end
