<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\V1\FileMoveRequest;
use App\Http\Requests\Api\V1\FileUploadRequest;
use App\Http\Requests\Api\V1\ShareLinkRequest;
use App\Services\FileStorageService;
use Illuminate\Http\Request;

class FileStorageController extends Controller
{
    /**
     * Upload file to temp
     */
    public function upload(FileUploadRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $service = new FileStorageService;
            $file = $service->upload(
                $request->file('file'),
                $request->input('description')
            );

            return $file;
        }, __('message.file_uploaded'));
    }

    /**
     * Move files from temp to folder
     */
    public function move(FileMoveRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $service = new FileStorageService;
            $files = $service->moveToFolder(
                $request->input('file_ids'),
                $request->input('folder_id'),
                $request->input('document_id'),
                $request->input('document_type')
            );

            return $files;
        }, __('message.files_moved'));
    }

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

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

    /**
     * List files
     */
    public function index(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $service = new FileStorageService;

            return $service->listFiles($request->all());
        });
    }

    /**
     * Get trash files
     */
    public function trash()
    {
        return ApiResponse::handle(function () {
            $service = new FileStorageService;

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

    /**
     * Download file
     */
    public function download(int $id)
    {
        $service = new FileStorageService;
        $file = $service->getFile($id);

        return $file->download();
    }

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

            return $service->deleteFile($id);
        }, __('message.file_deleted'));
    }

    /**
     * Restore file from trash
     */
    public function restore(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $service = new FileStorageService;

            return $service->restoreFile($id);
        }, __('message.file_restored'));
    }

    /**
     * Permanently delete file
     */
    public function permanentDelete(int $id)
    {
        return ApiResponse::handle(function () use ($id) {
            $service = new FileStorageService;
            $service->permanentDelete($id);

            return ['success' => true];
        }, __('message.file_permanently_deleted'));
    }

    /**
     * Create share link
     */
    public function createShareLink(ShareLinkRequest $request)
    {
        return ApiResponse::handle(function () use ($request) {
            $service = new FileStorageService;
            $link = $service->createShareLink(
                $request->input('file_id'),
                $request->input('expiry_hours', 24)
            );

            return [
                'token' => $link->token,
                'url' => url("/api/v1/files/share/{$link->token}"),
                'expires_at' => $link->expires_at,
            ];
        }, __('message.share_link_created'));
    }

    /**
     * Download file by share token (public, no auth)
     */
    public function downloadShared(string $token)
    {
        $file = FileStorageService::getFileByShareToken($token);

        return $file->download();
    }

    /**
     * Get storage usage
     */
    public function storageUsage()
    {
        return ApiResponse::handle(function () {
            $service = new FileStorageService;

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