package com.utopiaindustries.dao.ctp;

import com.utopiaindustries.model.ctp.CutPieceType;
import com.utopiaindustries.model.ctp.SkuCutPieces;
import com.utopiaindustries.util.KeyHolderFunctions;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class SkuCutPiecesDAO {
    private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private final String TABLE_NAME = "cut_to_pack.sku_cut_piece";
    private final String SELECT_QUERY = String.format( "SELECT * FROM %s WHERE id = :id", TABLE_NAME );
    private final String SELECT_ALL_QUERY = String.format( "SELECT * FROM %s ORDER BY id DESC", TABLE_NAME );
    private final String DELETE_QUERY = String.format( "DELETE FROM %s WHERE id = :id", TABLE_NAME );
    private final String INSERT_QUERY = String.format("INSERT INTO %s (sku, title) VALUES (:sku, :title) ON DUPLICATE KEY UPDATE sku = VALUES(sku), title = VALUES(title)", TABLE_NAME);
    private final String FIND_BY_SKU = String.format( "SELECT * FROM %s WHERE sku = :sku", TABLE_NAME );
    private final String CHECK_EXISTENCE_QUERY = String.format("SELECT COUNT(*) FROM %s WHERE title = :title AND sku = :sku", TABLE_NAME);

    public SkuCutPiecesDAO(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
    }

    // prepare query params
    private MapSqlParameterSource prepareInsertQueryParams(SkuCutPieces skuCutPieces ) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue( "id", skuCutPieces.getId() )
                .addValue( "sku", skuCutPieces.getSku())
                .addValue("title",skuCutPieces.getType());
        return params;
    }

    // find
    public SkuCutPieces find( long id ) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue( "id", id );
        return namedParameterJdbcTemplate.query( SELECT_QUERY, params, new SkuCutPieceRowMapper() )
                .stream()
                .findFirst()
                .orElse( new SkuCutPieces() );
    }

    public boolean doesExist(String type, String sku) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("title", type);
        params.addValue("sku", sku);
        Long count = namedParameterJdbcTemplate.queryForObject(CHECK_EXISTENCE_QUERY, params, Long.class);
        return count > 0;
    }



    //find by sku
    public List<SkuCutPieces> findBySku(String sku) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("sku", sku);
        return namedParameterJdbcTemplate.query(FIND_BY_SKU, params, new SkuCutPieceRowMapper());
    }

    // find all
    public List<CutPieceType> findAll() {
        return namedParameterJdbcTemplate.query( SELECT_ALL_QUERY, new CutPieceTypeRowMapper() );
    }

    // save
    public long save( SkuCutPieces skuCutPieces ) {
        KeyHolder keyHolder = new GeneratedKeyHolder();
        MapSqlParameterSource params = prepareInsertQueryParams( skuCutPieces );
        namedParameterJdbcTemplate.update( INSERT_QUERY, params, keyHolder );
        return KeyHolderFunctions.getKey( skuCutPieces.getId(), keyHolder );
    }

    // save all
    public int[] saveAll( List<SkuCutPieces> skuCutPieces ) {
        List<MapSqlParameterSource> batchArgs = new ArrayList<>();
        for ( SkuCutPieces cutPieceType: skuCutPieces ) {
            MapSqlParameterSource params = prepareInsertQueryParams( cutPieceType );
            batchArgs.add( params );
        }
        return namedParameterJdbcTemplate.batchUpdate( INSERT_QUERY, batchArgs.toArray(new MapSqlParameterSource[skuCutPieces.size()]) );
    }

    // delete
    public boolean delete( long id ) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue( "id", id );
        return namedParameterJdbcTemplate.update( DELETE_QUERY, params ) > 0;
    }
}
