package fr.eni.enchere.dal;

import fr.eni.enchere.bll.UserService;
import fr.eni.enchere.bo.Article;
import fr.eni.enchere.bo.SearchArticleCritere;
import fr.eni.enchere.bo.UserProfil;
import fr.eni.enchere.controllers.AccueilController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Primary;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
@Primary
public class ArticleRepositoryImpl implements ArticleRepository {

    private static final Logger logger = LoggerFactory.getLogger(ArticleRepositoryImpl.class);
    private JdbcTemplate jdbcTemplate;
    private NamedParameterJdbcTemplate namedJdbcTemplate;
    private UserService userService;

    private class ArticleRowMapper implements RowMapper<Article> {
        @Override
        public Article mapRow(ResultSet rs, int rowNum) throws SQLException {
            Article article = new Article();
            article.setId(rs.getInt("no_article"));
            article.setNom(rs.getString("nom_article"));
            article.setDesc(rs.getString("description"));
            article.setDateDebutEnch(rs.getDate("date_debut_encheres"));
            article.setDateFinEnch(rs.getDate("date_fin_encheres"));
            article.setPrixInitial(rs.getFloat("prix_initial"));
            article.setPrixVente(rs.getFloat("prix_vente"));

            UserProfil user = userService.utilisateurById(rs.getInt("no_utilisateur"));
            if (user != null) {
                article.setUtilisateur(user);
            } else {
                logger.error("erreur de l'utilisateur");
            }

            article.setNumCategorie(rs.getInt("no_categorie"));
            return article;
        }
    }

    public ArticleRepositoryImpl(JdbcTemplate jdbcTemplate, NamedParameterJdbcTemplate namedJdbcTemplate, UserService userService) {
        this.jdbcTemplate = jdbcTemplate;
        this.namedJdbcTemplate = namedJdbcTemplate;
        this.userService = userService;
    }

    @Override
    public List<Article> findAllArticle() {
        String sql = "SELECT * FROM ARTICLES_VENDUS";
        List<Article> articles = jdbcTemplate.query(sql, new ArticleRowMapper());
        return articles;
    }

    @Override
    public List<Article> searchArticle(SearchArticleCritere critere) {
        StringBuilder sql = new StringBuilder("SELECT * FROM ARTICLES_VENDUS WHERE 1 = 1");

        List<Object> params = new ArrayList<>();

        if (critere.getNoCategorie() != null) {
        sql.append(" AND no_categorie = ?");
        params.add(critere.getNoCategorie());
        }
        if (critere.getTitle() != null && !critere.getTitle().isEmpty()) {
            sql.append(" AND nom_article LIKE ?");
            params.add( '%' + critere.getTitle() + "%");
        }

        return jdbcTemplate.query(sql.toString(), new ArticleRowMapper(), params.toArray());
    }

    @Override
    public Article findArticleById(int id) {
        return null;
    }

    @Override
    public List<Article> findArticleByTitle(String title) {
        String sql = "SELECT * FROM ARTICLES_VENDUS WHERE nom_article LIKE ?";
        List<Article> articles = jdbcTemplate.query(sql, new ArticleRowMapper(), "%" + title + "%");
        return articles;
    }

    @Override
    public void saveArticle(Article article) {

    }

    @Override
    public void deleteArticle(int id) {

    }

    @Override
    public void updateArticle(int id) {

    }
}
