package de.accso.flexinale.data.services;

import de.accso.flexinale.data.model.Film;
import de.accso.flexinale.data.persistence.AbstractDao;
import de.accso.flexinale.data.persistence.FilmDao;
import de.accso.flexinale.shared_kernel.FlexinaleIllegalArgumentException;
import jakarta.transaction.Transactional;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Transactional
public non-sealed class FilmUploadService extends AbstractExcelDataUploadService<Film> {
    private static final Logger LOGGER = LoggerFactory.getLogger(FilmUploadService.class);

    @Autowired
    private FilmDao filmDao;

    @Override
    public AbstractDao<Film> getDao() {
        return filmDao;
    }

    @Override
    public String getNameOfExcelDataType() {
        return Film.class.getSimpleName();
    }

    @Override
    public Film createDataFromExcelRow(final Row excelRow) {
        String filmId = excelRow.getCell(0).getStringCellValue();

        Cell cellDauer = excelRow.getCell(3);
        int dauerInMinuten =
                switch(cellDauer.getCellType()) {
                    case NUMERIC -> (int) cellDauer.getNumericCellValue();
                    case STRING  -> Integer.parseInt(cellDauer.getStringCellValue());
                    default -> throw new FlexinaleIllegalArgumentException("Dauer is not a number");
                };

        Film film = new Film(
                filmId,
                excelRow.getCell(1).getStringCellValue(),
                excelRow.getCell(2).getStringCellValue(),
                dauerInMinuten
        );

        LOGGER.debug("New Film created " + film);

        return film;
    }
}
