package com.utopiaindustries.dao.ctp;

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

    private final NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private final String TABLE_NAME = "cut_to_pack.authorities";
    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 authority = :authority ORDER BY username DESC", TABLE_NAME );
    private final String INSERT_QUERY =String.format("INSERT INTO %s (username,authority) VALUES (:username,:authority)",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 AuthorityDAO(NamedParameterJdbcTemplate namedParameterJdbcTemplate) {
        this.namedParameterJdbcTemplate = namedParameterJdbcTemplate;
    }


    /*
    * prepare query params
    * */
    private MapSqlParameterSource prepareInsertQueryParams( Authority authority ){
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("username", authority.getUsername() );
        params.addValue("authority", authority.getAuthority() );
        return params;
    }


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

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

    /**
     * find by authority
     */
    public List<Authority> findByAuthority( String authority) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue( "authority", authority );
        return namedParameterJdbcTemplate.query( SELECT_BY_AUTHORITY_QUERY, params, new AuthorityRowMapper() );
    }
    /*
     * find by username
     * */
    public List<Authority> 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 AuthorityRowMapper() );
    }

    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<Authority> authorities ) {
        List<MapSqlParameterSource> batchArgs = new ArrayList<>();
        for ( Authority authority: authorities ) {
            MapSqlParameterSource params = prepareInsertQueryParams( authority );
            batchArgs.add( params );
        }
        return namedParameterJdbcTemplate.batchUpdate( INSERT_QUERY, batchArgs.toArray(new MapSqlParameterSource[authorities.size()]) );
    }
}
