package com.luojia_channel;

import com.luojia_channel.common.utils.RedisUtil;
import com.luojia_channel.modules.file.dto.CompleteUploadDTO;
import com.luojia_channel.modules.file.dto.UploadChunkDTO;
import com.luojia_channel.modules.file.service.FileService;
import com.luojia_channel.modules.file.service.impl.FileServiceImpl;
import com.luojia_channel.modules.file.utils.GeneratePathUtil;
import com.luojia_channel.modules.file.utils.ValidateFileUtil;
import io.minio.MinioClient;
import io.minio.PutObjectArgs;
import io.minio.RemoveObjectArgs;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mock.web.MockMultipartFile;

import java.util.concurrent.TimeUnit;

import static com.luojia_channel.modules.file.constants.FileConstant.CHUNK_PREFIX;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@SpringBootTest
public class FileServiceTests {
    @Autowired
    private FileServiceImpl fileService;

    @Autowired
    private MinioClient minioClient; // 模拟 MinIO 客户端

    @Autowired
    private RedisUtil redisUtil; // 模拟 Redis

    @Autowired
    private ValidateFileUtil validateFileUtil; // 模拟验证工具

    @Autowired
    private GeneratePathUtil generatePathUtil; // 模拟路径生成工具


    // 其他辅助变量（如测试文件内容、MD5 等）
    private static final String TEST_FILE_NAME = "test.jpg";
    private static final String TEST_FILE_MD5 = "test_md5";
    private static final String TEST_FILE_CONTENT = "test_content";
    private static final int TOTAL_CHUNKS = 2;
    @Test
    public void testUploadChunk_NormalCase() throws Exception {
        // 准备测试数据
        MockMultipartFile file = new MockMultipartFile(
                "file",
                TEST_FILE_NAME,
                "image",
                TEST_FILE_CONTENT.getBytes()
        );
        UploadChunkDTO chunkDTO = UploadChunkDTO.builder().build();
        chunkDTO.setFile(file);
        chunkDTO.setFileMd5(TEST_FILE_MD5);
        chunkDTO.setChunkNumber(0);
        chunkDTO.setTotalChunks(TOTAL_CHUNKS);
        chunkDTO.setFileType("image");
        chunkDTO.setFileName(TEST_FILE_NAME);

        fileService.uploadChunk(chunkDTO);

    }

    @Test
    public void testCompleteUpload_Success() throws Exception {
        // 准备合并前的环境
        CompleteUploadDTO completeDTO = CompleteUploadDTO.builder().build();
        completeDTO.setFileMd5(TEST_FILE_MD5);
        completeDTO.setTotalChunks(TOTAL_CHUNKS);
        completeDTO.setFileType("image");
        completeDTO.setFileName(TEST_FILE_NAME);
        //fileService.completeUpload(completeDTO);
        fileService.deleteChunks(TEST_FILE_MD5);
        //fileService.deleteBucket("chunks");
        /*
        try {
            minioClient.removeObject(
                    RemoveObjectArgs.builder()
                            .bucket("chunks")
                            .object("test_md5/chunk_0")
                            .build()
            );
            System.out.println("删除成功");
        } catch (Exception e) {
            System.err.println("删除失败: " + e.getMessage());
        }
        */
    }
}
