package com.utopiaindustries.dao.ctp;

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

	private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

	private final String TABLE_NAME = "cut_to_pack.inventory_transaction";
	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_datetime, generated_by, notes) VALUES (:id, :transaction_datetime, :generated_by, :notes) ON DUPLICATE KEY UPDATE transaction_datetime = VALUES(transaction_datetime), generated_by = VALUES(generated_by), notes = VALUES(notes)", TABLE_NAME );
	private final String SELECT_BY_IDS = String.format("SELECT * FROM %s WHERE id IN (:ids)", TABLE_NAME );

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

	// prepare query params
	private MapSqlParameterSource prepareInsertQueryParams( InventoryTransaction inventoryTransaction ) {
		MapSqlParameterSource params = new MapSqlParameterSource();
		params.addValue( "id", inventoryTransaction.getId() )
			.addValue( "transaction_datetime", inventoryTransaction.getTransactionDateTime() )
			.addValue( "generated_by", inventoryTransaction.getGeneratedBy() )
			.addValue( "notes", inventoryTransaction.getNotes() );
		return params;
	}

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

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

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

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

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

	public List<InventoryTransaction> 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 InventoryTransactionRowMapper() );
	}
}