<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\FolderStoreRequest;
use App\Http\Requests\Api\V1\FolderUpdateRequest;
use App\Services\FolderService;
use App\Utils\ApiResponse;
use Illuminate\Http\Request;

class FolderController extends Controller
{
    /**
     * List all folders
     */
    public function index()
    {
        return ApiResponse::handle(function () {
            $service = new FolderService;

            return $service->index();
        });
    }

    /**
     * Create new folder
     */
    public function store(FolderStoreRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $service = new FolderService;
            $folder = $service->store($request->validated());

            return $folder;
        }, __('message.folder_created'));
    }

    /**
     * Get folder by ID
     */
    public function show(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $service = new FolderService;

            return $service->show($id);
        });
    }

    /**
     * Update folder
     */
    public function update(int $id, FolderUpdateRequest $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            $service = new FolderService;
            $folder = $service->update($id, $request->validated());

            return $folder;
        }, __('message.folder_updated'));
    }

    /**
     * Soft delete/deactivate folder
     */
    public function destroy(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $service = new FolderService;

            return $service->destroy($id);
        }, __('message.folder_deleted'));
    }

    /**
     * Reorder folders
     */
    public function reorder(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $service = new FolderService;
            $orders = $service->reorder($request->input('orders', []));

            return $orders;
        }, __('message.folders_reordered'));
    }
}
