package de.avatic.lcc.controller.dev;

import de.avatic.lcc.config.filter.DevUserEmulationFilter;
import de.avatic.lcc.dto.error.CalculationJobDumpDTO;
import de.avatic.lcc.dto.users.UserDTO;
import de.avatic.lcc.repositories.error.DumpRepository;
import de.avatic.lcc.repositories.pagination.SearchQueryPagination;
import de.avatic.lcc.repositories.pagination.SearchQueryResult;
import de.avatic.lcc.repositories.users.UserRepository;
import de.avatic.lcc.service.transformer.users.UserTransformer;
import jakarta.servlet.http.HttpSession;
import jakarta.validation.constraints.Min;
import org.springframework.context.annotation.Profile;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Profile("dev")
@RequestMapping({"/api/dev", "/api/dev/"})
public class DevController {

    private final DumpRepository dumpRepository;
    private final UserRepository userRepository;
    private final UserTransformer userTransformer;

    public DevController(DumpRepository dumpRepository, UserRepository userRepository, UserTransformer userTransformer) {

        this.dumpRepository = dumpRepository;
        this.userRepository = userRepository;
        this.userTransformer = userTransformer;
    }

    @GetMapping({"/dump/{id}", "/dump/{id}/"})
    public ResponseEntity<CalculationJobDumpDTO> getDump(@PathVariable Integer id) {
        return ResponseEntity.ok(dumpRepository.getDump(id));
    }

    @GetMapping({"/dump/", "/dump"})
    public ResponseEntity<List<CalculationJobDumpDTO>> listDumps(
            @RequestParam(defaultValue = "20") @Min(1) int limit,
            @RequestParam(defaultValue = "1") @Min(1) int page) {

        var dump = dumpRepository.listDumps(new SearchQueryPagination(page, limit));

        return ResponseEntity.ok()
                .header("X-Total-Count", String.valueOf(dump.getTotalElements()))
                .header("X-Page-Count", String.valueOf(dump.getTotalPages()))
                .header("X-Current-Page", String.valueOf(page))
                .body(dump.toList());
    }

    @GetMapping({"/user"})
    public ResponseEntity<List<UserDTO>> listUser(@RequestParam(defaultValue = "20") @Min(1) int limit,
                                                  @RequestParam(defaultValue = "1") @Min(1) int page) {

        var users = SearchQueryResult.map(userRepository.listUsers(new SearchQueryPagination(page, limit)), userTransformer::toUserDTO);

        return ResponseEntity.ok()
                .header("X-Total-Count", String.valueOf(users.getTotalElements()))
                .header("X-Page-Count", String.valueOf(users.getTotalPages()))
                .header("X-Current-Page", String.valueOf(page))
                .body(users.toList());

    }

    @PostMapping({"/user", "/user/"})
    public ResponseEntity<Void> setActiveUser(@RequestBody UserDTO user, HttpSession session) {
        DevUserEmulationFilter.setEmulatedUserId(session, userRepository.getByEmail(user.getEmail()).getId());
        return ResponseEntity.ok().build();
    }


}
