package com.ruoyi.generator.service.front;

import com.ruoyi.generator.domain.GenProject;
import com.ruoyi.generator.domain.front.FrontProject;
import com.ruoyi.generator.domain.front.FrontProjectColumn;
import com.ruoyi.generator.domain.front.FrontProjectTable;
import com.ruoyi.generator.domain.front.dto.AppBlueprintDesign;
import org.junit.Test;

import java.util.Collections;

import static org.junit.Assert.assertEquals;

public class FrontProjectConverterTest
{
    @Test
    public void convertsFrontDraftToGenProject()
    {
        FrontProject project = new FrontProject();
        project.setProjectId(10L);
        project.setProjectName("车辆管理系统");
        project.setProjectFileName("vehicle-system");
        project.setPackageName("com.easycode.vehicle");
        project.setVersion("1.0.0");
        project.setFrontFramework("Vue3 + Element Plus");
        project.setBackFramework("Spring Boot + MyBatis");

        FrontProjectTable table = new FrontProjectTable();
        table.setTableId(20L);
        table.setTableName("car_info");
        table.setTableComment("车辆信息表");
        table.setClassName("CarInfo");
        table.setModuleName("vehicle");
        table.setBusinessName("car");
        table.setFunctionName("车辆信息");

        FrontProjectColumn column = new FrontProjectColumn();
        column.setColumnId(30L);
        column.setColumnName("car_id");
        column.setColumnComment("车辆ID");
        column.setColumnType("bigint(20)");
        column.setJavaType("Long");
        column.setJavaField("carId");
        column.setIsPk("1");
        column.setIsIncrement("1");
        table.setColumns(Collections.singletonList(column));
        project.setTables(Collections.singletonList(table));

        GenProject genProject = FrontProjectConverter.toGenProject(project);

        assertEquals("车辆管理系统", genProject.getProjectName());
        assertEquals("com.easycode.vehicle", genProject.getPackageName());
        assertEquals(1, genProject.getTables().size());
        assertEquals("car_info", genProject.getTables().get(0).getTableName());
        assertEquals("com.easycode.vehicle", genProject.getTables().get(0).getPackageName());
        assertEquals("carId", genProject.getTables().get(0).getColumns().get(0).getJavaField());
        assertEquals("carId", genProject.getTables().get(0).getPkColumn().getJavaField());
    }

    @Test
    public void convertsAppBlueprintToGenProject()
    {
        FrontProject project = new FrontProject();
        project.setProjectId(10L);
        project.setProjectName("Library");
        project.setPackageName("com.example.library");
        project.setAppBlueprint("{\"roles\":[{\"code\":\"reader\",\"name\":\"Reader\"}],\"frontendMenus\":[{\"code\":\"my_borrows\",\"name\":\"My Borrows\",\"path\":\"/my-borrows\",\"requiresLogin\":true,\"visibleRoles\":[\"reader\"],\"dataScope\":\"CURRENT_USER\"}],\"adminMenus\":[{\"code\":\"book_admin\",\"name\":\"Books\",\"path\":\"/books\",\"requiresLogin\":true,\"visibleRoles\":[\"admin\"],\"dataScope\":\"ROLE\"}]}");

        GenProject genProject = FrontProjectConverter.toGenProject(project);

        AppBlueprintDesign blueprint = genProject.getAppBlueprint();
        assertEquals("reader", blueprint.getRoles().get(0).getCode());
        assertEquals("my_borrows", genProject.getFrontendMenus().get(0).getCode());
        assertEquals("book_admin", genProject.getAdminMenus().get(0).getCode());
    }
}
