class InstitutionsController < ApplicationController
  before_action :set_institution, only: %i[ show edit update destroy ]



  # GET /institutions or /institutions.json
  def index
    @incomes = Income.all 
    @institutions = Institution.all
    @programs = Program.all
    @ziyaras = Ziyara.all
    @students = Student.all
    @institution_count = Institution.count
    @program_count     = Program.count
    @student_count     = Student.count
    @ziyara_count      = Ziyara.count

    @institutions = Institution.all
    @programs     = Program.all
    @ziyaras      = Ziyara.all
    @exclusive_traditional_records = ExclusiveTraditionalRecord.all
    @exclusive_traditional_records_count = ExclusiveTraditionalRecord.count
    @expenses = Expense.all
    @expenses = Expense.order(created_at: :desc)

  # Totals
  @total_expenses  = Expense.sum(:amount)
  @daily_expenses  = Expense.where(created_at: Time.current.all_day).sum(:amount)
  @weekly_expenses = Expense.where(created_at: Time.current.all_week).sum(:amount)
  @monthly_expenses = Expense.where(created_at: Time.current.all_month).sum(:amount)

  # Expense lists (MUST be here)
  @daily_expense_items  = Expense.where(created_at: Time.current.all_day)
  @weekly_expense_items = Expense.where(created_at: Time.current.all_week)
  @monthly_expense_items = Expense.where(created_at: Time.current.all_month)

  # Financial Summary
    @total_income = Income.sum(:amount)
    @total_expense = Expense.sum(:amount)
    @profit = Finance.profit
  # index is a collection action — do not attempt to load a single institution here
  @students = Student.all

  end

  # Use callbacks to share common setup or constraints between actions.
  def set_institution
    @institution = Institution.find_by(id: params[:institution_id])
  end


  # GET /institutions/1 or /institutions/1.json
  def show
    @institution = Institution.find(params[:id])
    @students = @institution.students 
  end

  # GET /institutions/contact
  def contact
  end

  # GET /institutions/new
  def new
    @institution = Institution.new
  end

  # GET /institutions/1/edit
  def edit
  end

  # POST /institutions or /institutions.json
  def create
    @institution = Institution.new(institution_params)

    respond_to do |format|
      if @institution.save
        format.html { redirect_to @institution, notice: "Institution was successfully created." }
        format.json { render :show, status: :created, location: @institution }
      else
        format.html { render :new, status: :unprocessable_entity }
        format.json { render json: @institution.errors, status: :unprocessable_entity }
      end
    end
  end


  # PATCH/PUT /institutions/1 or /institutions/1.json
  def update
    respond_to do |format|
      if @institution.update(institution_params)
        format.html { redirect_to @institution, notice: "Institution was successfully updated.", status: :see_other }
        format.json { render :show, status: :ok, location: @institution }
      else
        format.html { render :edit, status: :unprocessable_entity }
        format.json { render json: @institution.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /institutions/1 or /institutions/1.json
  def destroy
    @institution.destroy!

    respond_to do |format|
      format.html { redirect_to institutions_path, notice: "Institution was successfully destroyed.", status: :see_other }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_institution
      @institution = Institution.find(params[:id])
    end

    # Only allow a list of trusted parameters through.
    def institution_params
      params.require(:institution).permit(:name, :institution_type, :place)
    end


end
