class ExpensesController < ApplicationController
  before_action :set_expense, only: %i[ show edit update destroy ]

  # GET /expenses or /expenses.json
  def index
    @expenses = Expense.all
    @expenses = Expense.order(created_at: :desc)
    @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)
    @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)
  end

  # GET /expenses/1 or /expenses/1.json
  def show
    @expense = Expense.find_by(id: params[:id])
    redirect_to expenses_path, alert: "Expense not found" unless @expense

    # 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)

    # Lists
    @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)
  end

  # GET /expenses/report
  def report
    set_summary
    render :report
    @daily_expense_items = Expense.where(date: Date.today.all_day)
    @weekly_expense_items = Expense.where(date: Date.today.beginning_of_week..Date.today.end_of_week)
    @monthly_expense_items = Expense.where(date: Date.today.beginning_of_month..Date.today.end_of_month)

    @daily_expenses = @daily_expense_items.sum(:amount)
    @weekly_expenses = @weekly_expense_items.sum(:amount)
    @monthly_expenses = @monthly_expense_items.sum(:amount)

    # For chart
    @chart_labels = @monthly_expense_items.pluck(:date).map { |d| d.strftime("%d %b") }
    @chart_data = @monthly_expense_items.pluck(:amount)
  end

  # GET /expenses/new
  def new
    @expense = Expense.new
  end

  # GET /expenses/1/edit
  def edit
  end

  # POST /expenses or /expenses.json
  def create
    @expense = Expense.new(expense_params)

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

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

  # DELETE /expenses/1 or /expenses/1.json
  def destroy
    @expense.destroy!

    respond_to do |format|
      format.html { redirect_to expenses_path, notice: "Expense 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_expense
      @expense = Expense.find_by(id: params[:id])
      unless @expense
        redirect_to expenses_path, alert: "Expense not found"
      end
    end

    # Only allow a list of trusted parameters through.
    def expense_params
      params.require(:expense).permit(:amount, :date, :description)
    end
    def set_summary
      @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)

      @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)

      # Build chart data for the last 30 days (labels + totals per day)
      days_back = 29
      end_date = Date.current
      start_date = end_date - days_back
      labels = []
      data = []
      (start_date..end_date).each do |d|
        labels << d.strftime("%d %b")
        day_total = Expense.where(created_at: d.beginning_of_day..d.end_of_day).sum(:amount)
        data << day_total
      end
      @chart_labels = labels
      @chart_data = data
    end
end
