package ru.soune.nocopy.controller;


import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import ru.soune.nocopy.dto.docviewer.DockViewResponse;
import ru.soune.nocopy.entity.docviewer.DockView;
import ru.soune.nocopy.entity.file.FileEntity;
import ru.soune.nocopy.entity.user.User;
import ru.soune.nocopy.migration.S3MigrationTool;
import ru.soune.nocopy.repository.DockViewRepository;
import ru.soune.nocopy.repository.FileEntityRepository;
import ru.soune.nocopy.repository.UserRepository;
import ru.soune.nocopy.service.geo.GeoCountryService;

import java.util.List;
import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/api/admin/migration")
@RequiredArgsConstructor
@Slf4j
public class MigrationController {

    private final S3MigrationTool migrationTool;

    private final DockViewRepository dockViewRepository;

    private final GeoCountryService geoCountryService;

    private final UserRepository userRepository;

    private final FileEntityRepository fileEntityRepository;

    @PostMapping("/start")
    public ResponseEntity<String> startMigration() {
        log.info("Migration triggered via API");

        CompletableFuture.runAsync(() -> {
            try {
                migrationTool.migrationFilesToS3();
            } catch (Exception e) {
                log.error("Migration failed", e);
            }
        });

        return ResponseEntity.ok("Migration started. Check logs for progress.");
    }

    @PostMapping("/status")
    public ResponseEntity<String> getStatus() {
        return ResponseEntity.ok("Check application logs and migration_failed.txt for details");
    }

    @PostMapping("/fill-dock-view")
    public ResponseEntity<String> fillDockView() {
        List<DockView> dockViews = dockViewRepository.findAll();

        for (DockView dockView: dockViews) {
            String viewerIp = dockView.getViewerIp();
            Long viewerId = dockView.getViewerId();
            String geoEnName = geoCountryService.getCityByIp(viewerIp, "en") + "/" +
                    geoCountryService.getCountryByIp(viewerIp, "en");
            String geoRuName = geoCountryService.getCityByIp(viewerIp, "ru") + "/" +
                    geoCountryService.getCountryByIp(viewerIp, "ru");
            User user = userRepository.findById(viewerId).orElse(null);
            FileEntity file = fileEntityRepository.findByFileId(dockView.getFileId());

            dockView.setGeoNameRu(geoRuName);
            dockView.setGeoNameEn(geoEnName);
            dockView.setViewerName(user == null ? "guest" : user.getFullName());
            dockView.setDocName(file.getOriginalFileName());

            dockViewRepository.save(dockView);
        }

        return ResponseEntity.ok("fill old dock view");
    }
}
