package de.htwsaar.webshop.controller;

import de.htwsaar.webshop.model.SessionModel;
import de.htwsaar.webshop.repository.entities.Account;
import de.htwsaar.webshop.repository.entities.Session;
import de.htwsaar.webshop.service.AccountService;
import de.htwsaar.webshop.service.SessionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

import static de.htwsaar.webshop.config.ControllerPathConfig.SESSION_BASE;

@RestController
@Slf4j
public class SessionController {

    private final SessionService sessionService;
    private final AccountService accountService;

    @Autowired
    public SessionController(SessionService sessionService, AccountService accountService) {
        this.sessionService = sessionService;
        this.accountService = accountService;
    }

    @RequestMapping(value = SESSION_BASE, method = RequestMethod.POST, produces = "application/json")
    public ResponseEntity<SessionModel> create(@RequestParam String email, @RequestParam String password) {
        if (email == null || password == null) {
            log.warn("Got bad request, email and password");
            return ResponseEntity.badRequest().build();
        }
        Account acc = accountService.isValidLogin(email, password);
        if (acc == null) {
            log.warn("Got bad request, Account does not exist");
            return ResponseEntity.badRequest().build();
        }
        return ResponseEntity.ok(sessionService.create(acc).toModel());
    }


    @RequestMapping(value = SESSION_BASE, method = RequestMethod.DELETE, produces = "application/json")
    public ResponseEntity<Boolean> delete(@RequestParam String email, @RequestParam UUID token) {
        if (email == null || token == null) {
            log.warn("Got bad request, email and token");
            return ResponseEntity.badRequest().body(false);
        }
        Session session = sessionService.getByToken(token);
        if (!sessionService.isValid(session, email)) {
            log.warn("Got bad request, session is invalid");
            return ResponseEntity.badRequest().body(false);
        }
        sessionService.delete(session);
        return ResponseEntity.ok(true);
    }

    @RequestMapping(value = SESSION_BASE, method = RequestMethod.GET, produces = "application/json")
    public ResponseEntity<Boolean> isValid(@RequestParam String email, @RequestParam UUID token) {
        if (email == null || token == null) {
            log.warn("Got bad request, email and token");
            return ResponseEntity.badRequest().body(false);
        }
        Session session = sessionService.getByToken(token);
        if (session == null) {
            log.warn("Got bad request, session is invalid");
            return ResponseEntity.notFound().build();
        }
        if (sessionService.isValid(session, email)) {
            return ResponseEntity.ok(true);
        }
        return ResponseEntity.notFound().build();
    }

}
