package de.votian.services.controller;

import de.votian.services.dto.FilterAdminDto;
import de.votian.services.dto.GroupAdminDto;
import de.votian.services.dto.UserSessionDto;
import de.votian.services.security.AccessControlService;
import de.votian.services.service.GroupFilterAdminService;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/api/group-filter-admin")
public class GroupFilterAdminController {

    private final GroupFilterAdminService groupFilterAdminService;
    private final AccessControlService accessControlService;

    public GroupFilterAdminController(GroupFilterAdminService groupFilterAdminService,
                                      AccessControlService accessControlService) {
        this.groupFilterAdminService = groupFilterAdminService;
        this.accessControlService = accessControlService;
    }

    @GetMapping("/groups")
    public ResponseEntity<List<GroupAdminDto>> getGroups(@RequestParam Long hqId,
                                                         Authentication authentication) {
        UserSessionDto user = accessControlService.sessionUser(authentication);
        if (!accessControlService.isHeadquartersUser(user)
                || !accessControlService.canAccessHeadquarters(user, hqId)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        return ResponseEntity.ok(groupFilterAdminService.findGroups(hqId));
    }

    @PostMapping("/groups")
    public ResponseEntity<GroupAdminDto> createGroup(@RequestBody GroupRequest request,
                                                     Authentication authentication) {
        UserSessionDto user = accessControlService.sessionUser(authentication);
        if (request == null
                || request.hqId == null
                || !accessControlService.canManageHeadquarters(user)
                || !accessControlService.canAccessHeadquarters(user, request.hqId)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        return ResponseEntity.ok(groupFilterAdminService.createGroup(
                request.hqId,
                request.name,
                request.globalVisible
        ));
    }

    @PutMapping("/groups/{id}")
    public ResponseEntity<GroupAdminDto> updateGroup(@PathVariable Long id,
                                                     @RequestBody GroupRequest request,
                                                     Authentication authentication) {
        UserSessionDto user = accessControlService.sessionUser(authentication);
        if (request == null
                || request.hqId == null
                || !accessControlService.canManageHeadquarters(user)
                || !accessControlService.canAccessHeadquarters(user, request.hqId)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        return ResponseEntity.ok(groupFilterAdminService.updateGroup(
                id,
                request.hqId,
                request.name,
                request.globalVisible
        ));
    }

    @DeleteMapping("/groups/{id}")
    public ResponseEntity<Void> deleteGroup(@PathVariable Long id,
                                            @RequestParam Long hqId,
                                            Authentication authentication) {
        UserSessionDto user = accessControlService.sessionUser(authentication);
        if (!accessControlService.canManageHeadquarters(user)
                || !accessControlService.canAccessHeadquarters(user, hqId)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        groupFilterAdminService.deleteGroup(id, hqId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/filters")
    public ResponseEntity<List<FilterAdminDto>> getFilters(@RequestParam Long hqId,
                                                           Authentication authentication) {
        UserSessionDto user = accessControlService.sessionUser(authentication);
        if (!accessControlService.isHeadquartersUser(user)
                || !accessControlService.canAccessHeadquarters(user, hqId)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        return ResponseEntity.ok(groupFilterAdminService.findFilters());
    }

    @PostMapping("/filters")
    public ResponseEntity<FilterAdminDto> createFilter(@RequestBody FilterRequest request,
                                                       Authentication authentication) {
        UserSessionDto user = accessControlService.sessionUser(authentication);
        if (request == null
                || request.hqId == null
                || !accessControlService.canManageHeadquarters(user)
                || !accessControlService.canAccessHeadquarters(user, request.hqId)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        return ResponseEntity.ok(groupFilterAdminService.createFilter(
                request.shortCode,
                request.text,
                request.type,
                request.status,
                request.sort,
                request.longText
        ));
    }

    @PutMapping("/filters/{id}")
    public ResponseEntity<FilterAdminDto> updateFilter(@PathVariable Long id,
                                                       @RequestBody FilterRequest request,
                                                       Authentication authentication) {
        UserSessionDto user = accessControlService.sessionUser(authentication);
        if (request == null
                || request.hqId == null
                || !accessControlService.canManageHeadquarters(user)
                || !accessControlService.canAccessHeadquarters(user, request.hqId)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        return ResponseEntity.ok(groupFilterAdminService.updateFilter(
                id,
                request.text,
                request.type,
                request.status,
                request.sort,
                request.longText
        ));
    }

    @DeleteMapping("/filters/{id}")
    public ResponseEntity<Void> deleteFilter(@PathVariable Long id,
                                             @RequestParam Long hqId,
                                             Authentication authentication) {
        UserSessionDto user = accessControlService.sessionUser(authentication);
        if (!accessControlService.canManageHeadquarters(user)
                || !accessControlService.canAccessHeadquarters(user, hqId)) {
            return ResponseEntity.status(HttpStatus.FORBIDDEN).build();
        }
        groupFilterAdminService.deleteFilter(id);
        return ResponseEntity.ok().build();
    }

    public static class GroupRequest {
        public Long hqId;
        public String name;
        public boolean globalVisible;
    }

    public static class FilterRequest {
        public Long hqId;
        public String shortCode;
        public String text;
        public Integer type;
        public Integer status;
        public Integer sort;
        public String longText;
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public ResponseEntity<String> handleIllegalArgument(IllegalArgumentException exception) {
        return ResponseEntity.badRequest().body(exception.getMessage());
    }
}
