package fr.eni.enchere.dal;

import fr.eni.enchere.bo.Article;
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.List;

@Repository
@Primary
public class ArticleRepositoryImpl implements ArticleRepository {

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

    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"));
            article.setNumUtilisateur(rs.getInt("no_utilisateur"));
            article.setNumCategorie(rs.getInt("no_categorie"));
            return article;
        }
    }

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

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

    @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) {

    }
}
