package com.utopiaindustries.dao.ctp;

import com.utopiaindustries.model.ctp.InventorySummary;
import com.utopiaindustries.model.ctp.InventoryTransactionLeg;
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 InventoryTransactionLegDAO {

	private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

	private final String TABLE_NAME = "cut_to_pack.inventory_transaction_leg";
	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, transaction_id, item_id, sku, type, quantity, account_id, balance, transaction_leg_datetime, parent_document_type, parent_document_id, parent_document_piece_type) VALUES (:id, :transaction_id, :item_id, :sku, :type, :quantity, :account_id, :balance, :transaction_leg_datetime, :parent_document_type, :parent_document_id, :parent_document_piece_type) ON DUPLICATE KEY UPDATE transaction_id = VALUES(transaction_id), item_id = VALUES(item_id), sku = VALUES(sku), type = VALUES(type), quantity = VALUES(quantity), account_id = VALUES(account_id), balance = VALUES(balance), transaction_leg_datetime = VALUES(transaction_leg_datetime), parent_document_type = VALUES(parent_document_type), parent_document_id = VALUES(parent_document_id), parent_document_piece_type = VALUES(parent_document_piece_type)", TABLE_NAME );
	private final String SELECT_LAST_ACCOUNT_ID_AND_ITEM_ID = String.format( "SELECT * FROM %s WHERE account_id = :account_id AND item_id = :item_id AND parent_document_type= :parent_document_type AND parent_document_piece_type = :parent_document_piece_type ORDER BY id DESC LIMIT 1", TABLE_NAME );
	private final String SELECT_BY_ACCOUNT_ID = String.format( "SELECT * FROM %s WHERE account_id = :account_id ORDER BY id DESC" , TABLE_NAME );
	private final String SELECT_SUMMARY_BY_ACCOUNT_ID = String.format( "SELECT item_id, sku, parent_document_type,parent_document_piece_type," +
			" SUM(CASE WHEN type = 'IN' THEN quantity WHEN type = 'OUT' THEN -quantity ELSE 0 END) AS balance" +
			" FROM %s" +
			" WHERE account_id = :account_id" +
			" GROUP BY item_id, parent_document_type, parent_document_piece_type", TABLE_NAME );

	private final String SELECT_LAST_LEG_BY_TYPE_AND_PARENT_ID = String.format( "SELECT itl.* FROM %s itl" +
			" JOIN (" +
			" SELECT parent_document_id, parent_document_type, MAX(id) AS max_id" +
			" FROM %s" +
			" WHERE type = :type" +
			" AND parent_document_id IN (:parent_ids)" +
			" GROUP BY parent_document_id,parent_document_type )" +
			" last_in_transaction" +
			" ON itl.parent_document_id = last_in_transaction.parent_document_id" +
			" AND itl.id = last_in_transaction.max_id" +
			" AND itl.parent_document_type = last_in_transaction.parent_document_type" +
			" WHERE itl.type = :type" +
			" AND itl.parent_document_type = :parent_document_type"
			, TABLE_NAME, TABLE_NAME );

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

	// prepare query params
	private MapSqlParameterSource prepareInsertQueryParams( InventoryTransactionLeg inventoryTransactionLeg ) {
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue( "id", inventoryTransactionLeg.getId() )
			.addValue( "transaction_id", inventoryTransactionLeg.getTransactionId() )
			.addValue( "item_id", inventoryTransactionLeg.getItemId() )
			.addValue( "sku", inventoryTransactionLeg.getSku() )
			.addValue( "type", inventoryTransactionLeg.getType() )
			.addValue( "quantity", inventoryTransactionLeg.getQuantity() )
			.addValue( "account_id", inventoryTransactionLeg.getAccountId() )
			.addValue( "balance", inventoryTransactionLeg.getBalance() )
			.addValue( "transaction_leg_datetime", inventoryTransactionLeg.getTransactionLegDateTime() )
			.addValue( "parent_document_type", inventoryTransactionLeg.getParentDocumentType() )
			.addValue( "parent_document_id", inventoryTransactionLeg.getParentDocumentId() )
				.addValue("parent_document_piece_type", inventoryTransactionLeg.getParentDocumentPieceType() );
		return params;
	}

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

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

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

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

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

	public InventoryTransactionLeg findLastByAccountIdAndItemIdAndParentType( long accountId,
																			  long itemId,
																			  String parentType,
																			  String pieceType ){
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue( "account_id", accountId );
		params.addValue("item_id", itemId );
		params.addValue("parent_document_type", parentType );
		params.addValue("parent_document_piece_type", pieceType );
		return namedParameterJdbcTemplate.query( SELECT_LAST_ACCOUNT_ID_AND_ITEM_ID, params, new InventoryTransactionLegRowMapper() )
				.stream()
				.findFirst()
				.orElse( new InventoryTransactionLeg() );
	}

	public List<InventoryTransactionLeg> findByAccountId( long accountId ){
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue( "account_id", accountId );
		return namedParameterJdbcTemplate.query( SELECT_BY_ACCOUNT_ID, params, new InventoryTransactionLegRowMapper() );
	}

	public List<InventorySummary> findSummaryByAccountId( long accountId ){
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("account_id", accountId );
		return namedParameterJdbcTemplate.query( SELECT_SUMMARY_BY_ACCOUNT_ID, params, (rs, i) -> {
            InventorySummary summary = new InventorySummary();
			summary.setItemId( rs.getLong("item_id" ) );
			summary.setSku( rs.getString( "sku") );
			summary.setParentDocumentType( rs.getString("parent_document_type" ) );
			summary.setParentDocumentType( rs.getString("parent_document_type" ) );
			summary.setPieceType( rs.getString("parent_document_piece_type") );
			summary.setBalance( rs.getBigDecimal( "balance" ));
            return summary;
        });
	}

	public List<InventoryTransactionLeg> findLastTransactionByParentIdAndParentType( String type , List<Long> parentIds , String parentType){
		if ( parentIds == null || parentIds.isEmpty() ){
			return new ArrayList<>();
		}
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue("type", type );
		params.addValue("parent_ids", parentIds );
		params.addValue("parent_document_type", parentType );
		return namedParameterJdbcTemplate.query( SELECT_LAST_LEG_BY_TYPE_AND_PARENT_ID , params, new InventoryTransactionLegRowMapper() );
	}
}