package de.avatic.lcc.controller.configuration;

import com.azure.core.annotation.BodyParam;
import de.avatic.lcc.dto.configuration.apps.AppDTO;
import de.avatic.lcc.dto.configuration.apps.AppExchangeDTO;
import de.avatic.lcc.service.apps.AppsService;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/apps")
public class AppsController {


    private final AppsService appsService;

    public AppsController(AppsService appsService) {
        this.appsService = appsService;
    }

    @GetMapping({"", "/"})
    @PreAuthorize("hasRole('SERVICE')")
    public ResponseEntity<List<AppDTO>> listApps() {
        return ResponseEntity.ok(appsService.listApps());
    }

    @PostMapping({"", "/"})
    @PreAuthorize("hasRole('SERVICE')")
    public ResponseEntity<AppDTO> updateApp(@RequestBody AppDTO dto) {
        return ResponseEntity.ok(appsService.updateApp(dto));
    }

    @GetMapping({"/export/{id}", "/export/{id}/"})
    @PreAuthorize("hasRole('SERVICE')")
    public ResponseEntity<AppExchangeDTO> exportApp(@PathVariable Integer id) {
        return ResponseEntity.ok(appsService.exportApp(id));
    }

    @PostMapping({"/import/", "/import"})
    @PreAuthorize("hasRole('SERVICE')")
    public ResponseEntity<Boolean> importApp(@RequestBody AppExchangeDTO dto) {
        return ResponseEntity.ok(appsService.importApp(dto));
    }

    @DeleteMapping({"/{id}", "/{id}/"})
    @PreAuthorize("hasRole('SERVICE')")
    public ResponseEntity<Void> deleteApp(@PathVariable Integer id) {
        appsService.deleteApp(id);
        return ResponseEntity.ok().build();
    }


}
