package com.adityachandel.booklore.service;

import com.adityachandel.booklore.config.security.service.AuthenticationService;
import com.adityachandel.booklore.model.dto.MagicShelf;
import com.adityachandel.booklore.model.entity.MagicShelfEntity;
import com.adityachandel.booklore.repository.MagicShelfRepository;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@AllArgsConstructor
@Service
public class MagicShelfService {

    private final MagicShelfRepository repository;
    private final AuthenticationService authenticationService;

    public List<MagicShelf> getUserShelves() {
        Long userId = authenticationService.getAuthenticatedUser().getId();

        List<MagicShelf> shelves = repository.findAllByUserId(userId).stream()
                .map(this::toDto)
                .collect(Collectors.toList());

        List<Long> userShelfIds = shelves.stream().map(MagicShelf::getId).toList();

        List<MagicShelf> publicShelves = repository.findAllByIsPublicIsTrue().stream()
                .map(this::toDto)
                .filter(shelf -> !userShelfIds.contains(shelf.getId()))
                .toList();

        shelves.addAll(publicShelves);
        return shelves;
    }

    @Transactional
    public MagicShelf createOrUpdateShelf(MagicShelf dto) {
        Long userId = authenticationService.getAuthenticatedUser().getId();
        if (dto.getId() != null) {
            MagicShelfEntity existing = repository.findById(dto.getId()).orElseThrow(() -> new IllegalArgumentException("Shelf not found"));
            if (!existing.getUserId().equals(userId)) {
                throw new SecurityException("You are not authorized to update this shelf");
            }
            if (existing.isPublic() && !authenticationService.getAuthenticatedUser().getPermissions().isAdmin()) {
                throw new SecurityException("You are not authorized to update a public shelf");
            }
            existing.setName(dto.getName());
            existing.setIcon(dto.getIcon());
            existing.setFilterJson(dto.getFilterJson());
            existing.setPublic(dto.getIsPublic());
            return toDto(repository.save(existing));
        }
        if (repository.existsByUserIdAndName(userId, dto.getName())) {
            throw new IllegalArgumentException("A shelf with the same name already exists for this user.");
        }
        return toDto(repository.save(toEntity(dto, userId)));
    }

    @Transactional
    public void deleteShelf(Long id) {
        Long userId = authenticationService.getAuthenticatedUser().getId();
        MagicShelfEntity shelf = repository.findById(id).orElseThrow(() -> new IllegalArgumentException("Shelf not found"));
        if (!shelf.getUserId().equals(userId)) {
            throw new SecurityException("You are not authorized to delete this shelf");
        }
        repository.deleteById(id);
    }

    private MagicShelf toDto(MagicShelfEntity entity) {
        MagicShelf dto = new MagicShelf();
        dto.setId(entity.getId());
        dto.setName(entity.getName());
        dto.setIcon(entity.getIcon());
        dto.setFilterJson(entity.getFilterJson());
        dto.setIsPublic(entity.isPublic());
        return dto;
    }

    private MagicShelfEntity toEntity(MagicShelf dto, Long userId) {
        MagicShelfEntity entity = new MagicShelfEntity();
        entity.setId(dto.getId());
        entity.setName(dto.getName());
        entity.setIcon(dto.getIcon());
        entity.setFilterJson(dto.getFilterJson());
        entity.setPublic(dto.getIsPublic());
        entity.setUserId(userId);
        return entity;
    }

    public MagicShelf getShelf(Long id) {
        MagicShelfEntity shelf = repository.findById(id).orElseThrow(() -> new IllegalArgumentException("Shelf not found"));
        return toDto(shelf);
    }
}
