class Admin::ImagesController < ApplicationController
  before_action :authorize_admin
  before_action :set_image, only: [:show, :update, :destroy, :approve, :reject, :add_tags, :remove_tag]
  
  def index
    @q = Image.includes(:user, :tags).ransack(params[:q])
    @images = @q.result(distinct: true).with_attached_file.order(created_at: :desc).page(params[:page]).per(20)
    
    render inertia: 'admin/images/Index', props: {
      images: @images.as_json(include: [:user, :tags], methods: [:file_url]),
      filters: params[:q] || {},
      total_count: Image.count,
      pending_count: Image.pending.count,
      approved_count: Image.approved.count,
      rejected_count: Image.rejected.count
    }
  end

  def show
    render inertia: 'admin/images/Show', props: {
      image: @image.as_json(include: [:user, :tags], methods: [:file_url]),
      tags: Tag.all.pluck(:name)
    }
  end

  def update
    if @image.update(image_params)
      redirect_to admin_image_path(@image), notice: 'Image was successfully updated.'
    else
      render inertia: 'admin/images/Show', props: {
        image: @image.as_json(include: [:user, :tags], methods: [:file_url, :errors]),
        tags: Tag.all.pluck(:name),
        errors: @image.errors
      }, status: :unprocessable_entity
    end
  end

  def destroy
    @image.destroy
    redirect_to admin_images_path, notice: 'Image was successfully deleted.'
  end

  def pending
    @q = Image.pending.includes(:user, :tags).ransack(params[:q])
    @images = @q.result(distinct: true).with_attached_file.order(created_at: :desc).page(params[:page]).per(20)
    
    render inertia: 'admin/images/Pending', props: {
      images: @images.as_json(include: [:user, :tags], methods: [:file_url]),
      filters: params[:q] || {},
      total_count: Image.count,
      pending_count: Image.pending.count,
      approved_count: Image.approved.count,
      rejected_count: Image.rejected.count
    }
  end

  def approved
    @q = Image.approved.includes(:user, :tags).ransack(params[:q])
    @images = @q.result(distinct: true).with_attached_file.order(created_at: :desc).page(params[:page]).per(20)
    
    render inertia: 'admin/images/Approved', props: {
      images: @images.as_json(include: [:user, :tags], methods: [:file_url]),
      filters: params[:q] || {},
      total_count: Image.count,
      pending_count: Image.pending.count,
      approved_count: Image.approved.count,
      rejected_count: Image.rejected.count
    }
  end

  def rejected
    @q = Image.rejected.includes(:user, :tags).ransack(params[:q])
    @images = @q.result(distinct: true).with_attached_file.order(created_at: :desc).page(params[:page]).per(20)
    
    render inertia: 'admin/images/Rejected', props: {
      images: @images.as_json(include: [:user, :tags], methods: [:file_url]),
      filters: params[:q] || {},
      total_count: Image.count,
      pending_count: Image.pending.count,
      approved_count: Image.approved.count,
      rejected_count: Image.rejected.count
    }
  end

  def approve
    @image.approved!
    redirect_to admin_image_path(@image), notice: 'Image was successfully approved.'
  end

  def reject
    @image.rejected!
    redirect_to admin_image_path(@image), notice: 'Image was rejected.'
  end

  def add_tags
    if params[:tags].present?
      @image.add_tags(params[:tags].split(','))
      redirect_to admin_image_path(@image), notice: 'Tags were successfully added.'
    else
      redirect_to admin_image_path(@image), alert: 'No tags were provided.'
    end
  end

  def remove_tag
    tag = Tag.find(params[:tag_id])
    @image.tags.delete(tag)
    redirect_to admin_image_path(@image), notice: 'Tag was successfully removed.'
  end
  
  private
  
  def set_image
    @image = Image.find(params[:id])
  end
  
  def image_params
    params.require(:image).permit(:title)
  end
  
  def authorize_admin
    unless Current.user&.admin?
      redirect_to root_path, alert: 'You are not authorized to access this area.'
    end
  end
end