package com.utopiaindustries.dao.ctp;

import com.utopiaindustries.model.ctp.MasterBundle;
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 MasterBundleDAO {

	private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

	private final String TABLE_NAME = "cut_to_pack.master_bundle";
	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 (id, barcode,item_id, sku, created_by, created_at, job_card_id, is_received) VALUES (:id, :barcode, :item_id, :sku, :created_by, :created_at, :job_card_id, :is_received) ON DUPLICATE KEY UPDATE barcode = VALUES(barcode), item_id = VALUES(item_id), sku = VALUES(sku), created_by = VALUES(created_by), created_at = VALUES(created_at), job_card_id = VALUES(job_card_id),  is_received = VALUES(is_received)", TABLE_NAME );
	private final String SELECT_BY_LIMIT = String.format( "SELECT * FROM %s ORDER BY id DESC LIMIT :limit", TABLE_NAME );
	private final String SELECT_BY_TERM_AND_RECEIVED = String.format( "SELECT * FROM %s WHERE is_received = :is_received AND ( id LIKE :id OR barcode LIKE :barcode ) ", TABLE_NAME );
	private final String SELECT_BY_IDS = String.format( "SELECT * FROM %s WHERE id IN (:ids)", TABLE_NAME );


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

	// prepare query params
	private MapSqlParameterSource prepareInsertQueryParams( MasterBundle masterBundle ) {
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue( "id", masterBundle.getId() )
			.addValue( "barcode", masterBundle.getBarcode() )
			.addValue( "item_id", masterBundle.getItemId() )
			.addValue( "sku", masterBundle.getSku() )
			.addValue( "created_by", masterBundle.getCreatedBy() )
			.addValue( "created_at", masterBundle.getCreatedAt() )
			.addValue( "job_card_id", masterBundle.getJobCardId() )
			.addValue("is_received", masterBundle.getIsReceived() );
		return params;
	}

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

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

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

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

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

	public List<MasterBundle> findByQuery(String query ){
		return namedParameterJdbcTemplate.query( query, new MasterBundleRowMapper() );
	}

	public List<MasterBundle> findByLimit( Long count ){
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue( "limit",count );
		return namedParameterJdbcTemplate.query( SELECT_BY_LIMIT , params, new MasterBundleRowMapper() );
	}

	public List<MasterBundle> findByTermAndNotReceived( String term , boolean received ){
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("id" , term );
		params.addValue("barcode", "%"  + term + "%" );
		params.addValue("is_received", received );
		return namedParameterJdbcTemplate.query(SELECT_BY_TERM_AND_RECEIVED, params, new MasterBundleRowMapper() );
	}

	public List<MasterBundle> findByIds( List<Long> ids ){
		if( ids == null || ids.isEmpty() ) return new ArrayList<>();
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("ids", ids );
		return namedParameterJdbcTemplate.query( SELECT_BY_IDS , params, new MasterBundleRowMapper() );
	}
}