package com.njcn.rdms.module.project.framework.attachment;

import com.njcn.rdms.framework.test.core.ut.BaseMockitoUnitTest;
import com.njcn.rdms.module.project.dal.dataobject.attachment.AttachmentItem;
import com.njcn.rdms.module.system.api.file.FileApi;
import com.njcn.rdms.module.system.api.file.dto.FileRespDTO;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;

import java.util.List;

import static com.njcn.rdms.framework.common.pojo.CommonResult.success;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class AttachmentFileIdResolverTest extends BaseMockitoUnitTest {

    @InjectMocks
    private AttachmentFileIdResolver resolver;
    @Mock
    private FileApi fileApi;

    @Test
    void resolve_whenAttachmentIdBlank_shouldFillByUrl() {
        String url = "http://oss.example.com/task/20260511/test.txt?X-Amz-Signature=abc";
        AttachmentItem attachment = new AttachmentItem();
        attachment.setUrl(url);
        FileRespDTO file = new FileRespDTO();
        file.setId(2053737548535996418L);
        when(fileApi.getFileByUrl(url)).thenReturn(success(file));

        resolver.resolve(List.of(attachment));

        assertEquals("2053737548535996418", attachment.getId());
    }

    @Test
    void resolve_whenAttachmentIdPresent_shouldNotQueryFileApi() {
        AttachmentItem attachment = new AttachmentItem();
        attachment.setId("10001");
        attachment.setUrl("http://oss.example.com/task/20260511/test.txt");

        resolver.resolve(List.of(attachment));

        verify(fileApi, never()).getFileByUrl(attachment.getUrl());
        assertEquals("10001", attachment.getId());
    }

}
