package de.votian.services.controller;

import de.votian.services.dto.LonghaulDashboardDto;
import de.votian.services.dto.LonghaulJobDetailDto;
import de.votian.services.dto.UserSessionDto;
import de.votian.services.security.AccessControlService;
import de.votian.services.service.LonghaulService;
import de.votian.services.service.LonghaulRemoteDbService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/longhaul")
public class LonghaulController {

    private final LonghaulService longhaulService;
    private final LonghaulRemoteDbService longhaulRemoteDbService;
    private final AccessControlService accessControlService;

    public LonghaulController(LonghaulService longhaulService,
                              LonghaulRemoteDbService longhaulRemoteDbService,
                              AccessControlService accessControlService) {
        this.longhaulService = longhaulService;
        this.longhaulRemoteDbService = longhaulRemoteDbService;
        this.accessControlService = accessControlService;
    }

    @GetMapping("/dashboard")
    public ResponseEntity<LonghaulDashboardDto> getDashboard(@RequestParam Long hqId,
                                                             @RequestParam(required = false) Boolean remoteDb,
                                                             Authentication authentication) {
        UserSessionDto user = accessControlService.sessionUser(authentication);
        if (!accessControlService.canViewLonghaul(user) || !accessControlService.canAccessHeadquarters(user, hqId)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        boolean remoteDbAvailable = accessControlService.canUseLonghaulRemoteDb(user)
                && longhaulRemoteDbService.isConfigured();
        if (Boolean.TRUE.equals(remoteDb) && !remoteDbAvailable) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        LonghaulDashboardDto dto = longhaulService.getDashboard(hqId, Boolean.TRUE.equals(remoteDb));
        dto.setRemoteDbAvailable(remoteDbAvailable);
        return ResponseEntity.ok(dto);
    }

    @PutMapping("/jobs/{jobId}/return")
    public ResponseEntity<Void> returnToHeadquarters(@PathVariable Long jobId,
                                                     @RequestParam Long hqId,
                                                     @RequestParam(required = false) Boolean remoteDb,
                                                     Authentication authentication) {
        UserSessionDto user = accessControlService.sessionUser(authentication);
        if (!accessControlService.canManageLonghaul(user) || !accessControlService.canAccessHeadquarters(user, hqId)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        boolean remoteDbAvailable = accessControlService.canUseLonghaulRemoteDb(user)
                && longhaulRemoteDbService.isConfigured();
        if (Boolean.TRUE.equals(remoteDb) && !remoteDbAvailable) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        longhaulService.returnJobToHeadquarters(hqId, jobId, Boolean.TRUE.equals(remoteDb));
        return ResponseEntity.ok().build();
    }

    @GetMapping("/jobs/{jobId}/detail")
    public ResponseEntity<LonghaulJobDetailDto> getJobDetail(@PathVariable Long jobId,
                                                             @RequestParam Long hqId,
                                                             @RequestParam(required = false) Boolean remoteDb,
                                                             Authentication authentication) {
        UserSessionDto user = accessControlService.sessionUser(authentication);
        if (!accessControlService.canViewLonghaul(user) || !accessControlService.canAccessHeadquarters(user, hqId)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        boolean remoteDbAvailable = accessControlService.canUseLonghaulRemoteDb(user)
                && longhaulRemoteDbService.isConfigured();
        if (Boolean.TRUE.equals(remoteDb) && !remoteDbAvailable) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        return ResponseEntity.ok(longhaulService.getJobDetail(hqId, jobId, Boolean.TRUE.equals(remoteDb)));
    }
}
