package de.ph87.isabell_und_timo.picture;

import jakarta.annotation.Nullable;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.server.ResponseStatusException;

import java.time.ZonedDateTime;

@Slf4j
@Service
@RequiredArgsConstructor
public class PictureService {

  private final PictureRepository pictureRepository;

  @NonNull
  @Transactional
  public PictureInternal create(
    @Nullable final ZonedDateTime date,
    @Nullable final String make,
    @Nullable final String model,
    final int width,
    final int height,
    final long size,
    @NonNull final String originalFilename,
    @NonNull final String uploadFilename,
    @NonNull final ZonedDateTime uploadDate,
    @NonNull final String uploadAddress,
    @Nullable final String uploadAgent
  ) {
    final Picture picture = pictureRepository.save(new Picture(originalFilename, date, size, width, height, make, model, uploadFilename, uploadDate, uploadAddress, uploadAgent));
    log.info("Uploaded: {}", picture);
    return new PictureInternal(picture);
  }

  @Transactional
  public void visible(final @NonNull String uuid, final boolean visible) {
    final Picture picture = pictureRepository.findById(uuid).orElseThrow(() -> new ResponseStatusException(HttpStatus.NOT_FOUND));
    picture.setVisible(visible);
    log.info("Visible: {}", picture);
  }

}
