package com.cloudhandson.ddsbackoffice.web;

import com.cloudhandson.ddsbackoffice.config.DdsProperties;
import com.cloudhandson.ddsbackoffice.service.DdsProtectionStatusService;
import com.cloudhandson.ddsbackoffice.service.DdsProtectionValidationService;
import com.cloudhandson.ddsbackoffice.service.DdsProtectionSchemaService;
import com.cloudhandson.ddsbackoffice.service.DdsSqlEvidenceHistory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.view.RedirectView;

/** DDS protection status, deliberately separate from the legacy VPD route. */
@Controller
public class DdsProtectionController {

  private final DdsProperties properties;
  private final DdsProtectionStatusService protectionStatusService;
  private final DdsProtectionValidationService validationService;
  private final DdsProtectionSchemaService schemaService;
  private final DdsSqlEvidenceHistory evidenceHistory;

  public DdsProtectionController(
      DdsProperties properties,
      DdsProtectionStatusService protectionStatusService,
      DdsProtectionValidationService validationService,
      DdsProtectionSchemaService schemaService,
      DdsSqlEvidenceHistory evidenceHistory
  ) {
    this.properties = properties;
    this.protectionStatusService = protectionStatusService;
    this.validationService = validationService;
    this.schemaService = schemaService;
    this.evidenceHistory = evidenceHistory;
  }

  @GetMapping("/dds-protection")
  public String page(Model model) {
    model.addAttribute("ddsVectorObject", properties.vectorObject());
    model.addAttribute("protection", protectionStatusService.overview());
    return "vpd-policies";
  }

  @GetMapping("/dds-evidence")
  public String evidence(Model model) {
    model.addAttribute("evidenceEntries", evidenceHistory.recent());
    return "dds-evidence";
  }

  @GetMapping("/dds-admin")
  public String admin(Model model) {
    model.addAttribute("ddsVectorObject", properties.vectorObject());
    return "dds-admin";
  }

  @GetMapping("/dds-protection/direct")
  public String directComparison(Model model) {
    model.addAttribute("directPaths", protectionStatusService.directComparison());
    return "fragments/dds-protection-direct :: directComparison";
  }

  @PostMapping("/dds-protection/verify/service")
  public RedirectView verifyService(org.springframework.web.servlet.mvc.support.RedirectAttributes attributes) {
    try {
      var result = validationService.runServiceFixtures();
      attributes.addFlashAttribute(result.passed() ? "successMessage" : "errorMessage", result.message());
    } catch (RuntimeException exception) {
      attributes.addFlashAttribute("errorMessage", "DDS 보호 검증을 실행하지 못했습니다. fixture와 DB 설정을 확인하세요.");
    }
    return new RedirectView("/dds-protection#service-verify");
  }

  @PostMapping("/dds-protection/verify/direct")
  public String verifyDirect(Model model) {
    validationService.runDirectFixtures();
    model.addAttribute("directPaths", protectionStatusService.directComparison());
    return "fragments/dds-protection-direct :: directComparison";
  }

  @PostMapping("/dds-protection/setup")
  public RedirectView setup(org.springframework.web.servlet.mvc.support.RedirectAttributes attributes) {
    try {
      schemaService.initialize();
      attributes.addFlashAttribute("successMessage", "DDS 검증 이력과 fixture 구성을 완료했습니다.");
    } catch (RuntimeException exception) {
      attributes.addFlashAttribute("errorMessage", "DDS 검증 구성을 완료하지 못했습니다. DB 권한과 선행 DDS 객체를 확인하세요.");
    }
    return new RedirectView("/dds-protection");
  }

  /** Existing bookmarks resolve to the DDS canonical route without retaining VPD in the UI. */
  @GetMapping("/vpd-policies")
  public RedirectView legacyPolicies() {
    return new RedirectView("/dds-protection");
  }

  @GetMapping("/vpd-filter-policies")
  public RedirectView filterPolicies() {
    return new RedirectView("/dds-protection");
  }
}
