package com.kamco.cd.kamcoback.common.api;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/api/nfs-test")
public class NfsTestApiController {

  private static final String ORIGINAL_IMAGES_PATH = "/app/original-images";
  private static final String MODEL_OUTPUTS_PATH = "/app/model-outputs";
  private static final String TRAIN_DATASET_PATH = "/app/train-dataset";

  @GetMapping("/original-images")
  public Map<String, Object> listOriginalImages() {
    return listDirectory(ORIGINAL_IMAGES_PATH);
  }

  @GetMapping("/model-outputs")
  public Map<String, Object> listModelOutputs() {
    return listDirectory(MODEL_OUTPUTS_PATH);
  }

  @GetMapping("/train-dataset")
  public Map<String, Object> listTrainDataset() {
    return listDirectory(TRAIN_DATASET_PATH);
  }

  @GetMapping("/all")
  public Map<String, Object> listAllDirectories() {
    Map<String, Object> result = new HashMap<>();
    result.put("originalImages", listDirectory(ORIGINAL_IMAGES_PATH));
    result.put("modelOutputs", listDirectory(MODEL_OUTPUTS_PATH));
    result.put("trainDataset", listDirectory(TRAIN_DATASET_PATH));
    return result;
  }

  private Map<String, Object> listDirectory(String directoryPath) {
    Map<String, Object> result = new HashMap<>();

    try {
      Path path = Paths.get(directoryPath);
      File directory = path.toFile();

      result.put("path", directoryPath);
      result.put("exists", directory.exists());
      result.put("isDirectory", directory.isDirectory());
      result.put("canRead", directory.canRead());
      result.put("canWrite", directory.canWrite());

      if (directory.exists() && directory.isDirectory()) {
        List<Map<String, Object>> files = new ArrayList<>();

        File[] fileArray = directory.listFiles();
        if (fileArray != null) {
          for (File file : fileArray) {
            Map<String, Object> fileInfo = new HashMap<>();
            fileInfo.put("name", file.getName());
            fileInfo.put("isDirectory", file.isDirectory());
            fileInfo.put("size", file.length());
            fileInfo.put("lastModified", file.lastModified());
            fileInfo.put("canRead", file.canRead());
            fileInfo.put("canWrite", file.canWrite());
            files.add(fileInfo);
          }
        }

        result.put("files", files);
        result.put("fileCount", files.size());
      } else {
        result.put("files", new ArrayList<>());
        result.put("fileCount", 0);
        result.put("error", "Directory does not exist or is not accessible");
      }

    } catch (Exception e) {
      log.error("Error listing directory: {}", directoryPath, e);
      result.put("error", e.getMessage());
      result.put("exception", e.getClass().getSimpleName());
    }

    return result;
  }
}
