package ru.kayashov.bar.controller;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import ru.kayashov.bar.controller.dto.VisitorResponseDto;
import ru.kayashov.bar.controller.dto.cocktail.ReceiptResponseDto;
import ru.kayashov.bar.model.entity.Category;
import ru.kayashov.bar.model.entity.Glass;
import ru.kayashov.bar.model.entity.Unit;
import ru.kayashov.bar.model.entity.UserRole;
import ru.kayashov.bar.model.entity.Visitor;
import ru.kayashov.bar.repository.UnitRepository;
import ru.kayashov.bar.service.SessionService;
import ru.kayashov.bar.service.VisitorService;

import java.util.Arrays;
import java.util.List;

@Slf4j
@CrossOrigin(origins = {"*"})
@RestController
@RequestMapping("/api/bar/")
@RequiredArgsConstructor
public class BarController {

    private final SessionService sessionService;
    private final VisitorService visitorService;
    private final UnitRepository unitRepository;

    @GetMapping("/units")
    public List<Unit> getUnitList() {
        return unitRepository.findAll();
    }

    @GetMapping("/glass")
    public List<String> getGlass() {
        return Arrays.stream(Glass.values()).map(Glass::getName).toList();
    }

    @GetMapping("/category")
    public List<String> getCategory() {
        return Arrays.stream(Category.values()).map(Category::getName).toList();
    }

    @GetMapping("/receipt")
    public List<ReceiptResponseDto> getReceipt(@RequestParam("id") Long id) {
        return sessionService.getReceiptList(id);
    }

    @GetMapping("/getMe")
    public VisitorResponseDto getMe() {
        Visitor visitor = visitorService.getCurrentVisitor();
        String role;
        role = UserRole.ADMIN.toString();
        VisitorResponseDto dto = VisitorResponseDto.mapToDto(visitor, true, role, true);
        log.info("Запрос информации о пользователе: {}-{} {}, вошел в бар",
                dto.getId(),
                dto.getName().strip(),
                dto.getLastName() != null ? dto.getLastName().strip() : "");
        return dto;
    }
}
