package com.utopiaindustries.dao.ctp;

import com.utopiaindustries.model.ctp.UserInventoryAccount;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

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

@Repository
public class UserInventoryAccountDAO {

    private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private final String TABLE_NAME = "cut_to_pack.user_inventory_account";
    private final String SELECT_ALL_QUERY = String.format( "SELECT * FROM %s ORDER BY username DESC", TABLE_NAME );
    private final String SELECT_BY_USERNAME_QUERY = String.format( "SELECT * FROM %s WHERE username = :username ORDER BY username DESC", TABLE_NAME );
    private final String SELECT_BY_AUTHORITY_QUERY = String.format( "SELECT * FROM %s WHERE account_id = :account_id ORDER BY username DESC", TABLE_NAME );
    private final String INSERT_QUERY =String.format("INSERT INTO %s (username,account_id) VALUES (:username,:account_id)",TABLE_NAME);
    private final String DELETE_QUERY=String.format("DELETE FROM %s WHERE username = :username",TABLE_NAME);
    private final String SELECT_BY_USERNAMES = String.format("SELECT * FROM %s WHERE username IN (:usernames)" , TABLE_NAME );

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

    /*
     * prepare query params
     * */
    private MapSqlParameterSource prepareInsertQueryParams( UserInventoryAccount userInventoryAccount ){
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("username", userInventoryAccount.getUsername() );
        params.addValue("account_id", userInventoryAccount.getAccountId() );
        return params;
    }


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

    /**
     * find by username
     */
    public List<UserInventoryAccount> findByUsername( String username ) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue( "username", username );
        return namedParameterJdbcTemplate.query( SELECT_BY_USERNAME_QUERY, params, new UserInventoryAccountRowMapper() );
    }

    /**
     * find by authority
     */
    public List<UserInventoryAccount> findByAccountId( long accountId ) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue( "account_id", accountId );
        return namedParameterJdbcTemplate.query( SELECT_BY_AUTHORITY_QUERY, params, new UserInventoryAccountRowMapper() );
    }

    /*
    * find by username
    * */
    public List<UserInventoryAccount> findByUsernames( List<String> usernames ) {
        if( usernames == null || usernames.isEmpty() ) {
            return new ArrayList<>();
        }
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue( "usernames", usernames );
        return namedParameterJdbcTemplate.query( SELECT_BY_USERNAMES, params, new UserInventoryAccountRowMapper() );
    }

    /*
    *  delete by username
    * */
    public boolean deleteByUsername( String username ){
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue( "username", username );
        return namedParameterJdbcTemplate.update( DELETE_QUERY, params) > 0;
    }

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