<?php

namespace App\Http\Controllers\System\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Services\System\UploadService;

class Upload extends Controller
{
    protected $uploadService;

    public function __construct(UploadService $uploadService)
    {
        $this->uploadService = $uploadService;
    }

    public function upload(Request $request)
    {
        try {
            $request->validate([
                'file' => 'required|file|max:10240',
            ]);

            $file = $request->file('file');
            $directory = $request->input('directory', 'uploads');
            $options = [
                'compress' => $request->input('compress', false),
                'quality' => $request->input('quality', 80),
                'width' => $request->input('width'),
                'height' => $request->input('height'),
            ];

            $result = $this->uploadService->upload($file, $directory, $options);
            return response()->json([
                'code' => 200,
                'message' => '上传成功',
                'data' => $result
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 400,
                'message' => $e->getMessage(),
                'data' => null
            ], 400);
        }
    }

    public function uploadMultiple(Request $request)
    {
        try {
            $request->validate([
                'files' => 'required|array',
                'files.*' => 'file|max:10240',
            ]);

            $files = $request->file('files');
            $directory = $request->input('directory', 'uploads');
            $options = [
                'compress' => $request->input('compress', false),
                'quality' => $request->input('quality', 80),
                'width' => $request->input('width'),
                'height' => $request->input('height'),
            ];

            $results = $this->uploadService->uploadMultiple($files, $directory, $options);
            return response()->json([
                'code' => 200,
                'message' => '上传成功',
                'data' => $results
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 400,
                'message' => $e->getMessage(),
                'data' => null
            ], 400);
        }
    }

    public function uploadBase64(Request $request)
    {
        try {
            $request->validate([
                'base64' => 'required|string',
            ]);

            $base64 = $request->input('base64');
            $directory = $request->input('directory', 'uploads');
            $fileName = $request->input('file_name');

            $result = $this->uploadService->uploadBase64($base64, $directory, $fileName);
            return response()->json([
                'code' => 200,
                'message' => '上传成功',
                'data' => $result
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 400,
                'message' => $e->getMessage(),
                'data' => null
            ], 400);
        }
    }

    public function delete(Request $request)
    {
        try {
            $request->validate([
                'path' => 'required|string',
            ]);

            $path = $request->input('path');
            $this->uploadService->delete($path);
            return response()->json([
                'code' => 200,
                'message' => '删除成功',
                'data' => null
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 400,
                'message' => $e->getMessage(),
                'data' => null
            ], 400);
        }
    }

    public function batchDelete(Request $request)
    {
        try {
            $request->validate([
                'paths' => 'required|array',
            ]);

            $paths = $request->input('paths', []);
            $this->uploadService->deleteMultiple($paths);
            return response()->json([
                'code' => 200,
                'message' => '批量删除成功',
                'data' => null
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'code' => 400,
                'message' => $e->getMessage(),
                'data' => null
            ], 400);
        }
    }
}
