package com.healthcare.ohctech.controller;

import com.healthcare.ohctech.dto.DeviceParameterDto;
import com.healthcare.ohctech.entity.DeviceParameter;
import com.healthcare.ohctech.service.impl.AuthServiceImpl;
import com.healthcare.ohctech.service.impl.DeviceParameterServiceImpl;
import com.healthcare.ohctech.util.PaginationUtil;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/parameters")
public class DeviceParameterController {
    @Autowired
    private DeviceParameterServiceImpl deviceParameterServiceImpl;

    @Autowired
    private AuthServiceImpl authServiceImpl;

    @GetMapping("/{parameterId}")
    public ResponseEntity<DeviceParameter> getParameterById(@PathVariable Long paraId) {
        DeviceParameter deviceParameter = deviceParameterServiceImpl.getParameterById(paraId);
        return new ResponseEntity<>(deviceParameter, HttpStatus.OK);
    }

    @GetMapping
    public ResponseEntity<Map<String, Object>> getAllParameters(@RequestParam(required = false) Integer page,
                                                                          @RequestParam(required = false) Integer size,
                                                                          @RequestParam(required = false) String sortBy,
                                                                          @RequestParam(required = false) String sortOrder) {
        Pageable pageable = PaginationUtil.getPageableWithDefaults(page, size, sortBy, sortOrder);
        Page<DeviceParameter> deviceParameterPage = deviceParameterServiceImpl.getAllParameters(pageable);
        Map<String, Object> response = PaginationUtil.getPageResponse(deviceParameterPage);
        return new ResponseEntity<>(response, HttpStatus.OK);
    }

    @PostMapping
    public ResponseEntity<String> addParameter(@Valid @RequestBody List<DeviceParameterDto> deviceParameterDtos) {
        Long userId = authServiceImpl.getCurrentUserId();
        deviceParameterServiceImpl.addParameter(deviceParameterDtos, userId);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping("/{parameterId}")
    public ResponseEntity<String> updateParameter(@Valid @RequestBody DeviceParameterDto deviceParameterDtos) {
        Long userId = authServiceImpl.getCurrentUserId();
        deviceParameterServiceImpl.updateParameter(deviceParameterDtos,userId);
        return new ResponseEntity<>( HttpStatus.OK);
    }

    @DeleteMapping("/{parameterId}")
    public ResponseEntity<String> deleteParameter(@PathVariable Long paraId) {
        deviceParameterServiceImpl.deleteParameter(paraId);
        return new ResponseEntity<>( HttpStatus.OK);
    }

}