package com.utopiaindustries.dao.ctp;

import com.utopiaindustries.model.ctp.JobCard;
import com.utopiaindustries.model.ctp.PurchaseOrderCTP;
import com.utopiaindustries.util.KeyHolderFunctions;
import org.springframework.beans.factory.annotation.Autowired;
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 PurchaseOrderCTPDao {

    @Autowired
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    private final String TABLE_NAME = "cut_to_pack.purchase_order";
    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 SELECT_ALL_QUERY_WITH_LIMIT = String.format( "SELECT * FROM %s ORDER BY id DESC limit :limit", 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, purchase_order_code, purchase_order_quantity, purchase_order_quantity_required, article_name, created_by, status) " +
                    "VALUES (:id, :purchase_order_code, :purchase_order_quantity, :purchase_order_quantity_required, :article_name, :created_by, :status) " +
                    "ON DUPLICATE KEY UPDATE " +
                    "purchase_order_code = VALUES(purchase_order_code), " +
                    "purchase_order_quantity = VALUES(purchase_order_quantity), " +
                    "purchase_order_quantity_required = VALUES(purchase_order_quantity_required), " +
                    "article_name = VALUES(article_name), " +
                    "created_by = VALUES(created_by), " +
                    "status = VALUES(status)",
            TABLE_NAME);
    private final String SELECT_BY_LIMIT = String.format( "SELECT * FROM %s WHERE created_by = :created_by ORDER BY id ASC limit :limit", TABLE_NAME );


    // prepare query params
    private MapSqlParameterSource prepareInsertQueryParams(PurchaseOrderCTP purchaseOrderCTP) {
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("id", purchaseOrderCTP.getId())
                .addValue("purchase_order_code", purchaseOrderCTP.getPurchaseOrderCode())
                .addValue("purchase_order_quantity", purchaseOrderCTP.getPurchaseOrderQuantity())
                .addValue("purchase_order_quantity_required", purchaseOrderCTP.getPurchaseOrderQuantityRequired())
                .addValue("article_name", purchaseOrderCTP.getArticleName())
                .addValue("created_by", purchaseOrderCTP.getCreatedBy())
                .addValue("status", purchaseOrderCTP.getStatus());
        return params;
    }


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


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

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

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

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

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

    public List<PurchaseOrderCTP> findByUserAndLimit(String createdBy, Long limit ){
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("limit", limit.intValue() );
        params.addValue("created_by", createdBy );
        return namedParameterJdbcTemplate.query( SELECT_BY_LIMIT, params, new PurchaseOrderCTPRowMapper() );
    }

    public List<PurchaseOrderCTP> findByAllWithLimit(Long limit){
        MapSqlParameterSource params = new MapSqlParameterSource();
        params.addValue("limit", limit.intValue());
        return namedParameterJdbcTemplate.query( SELECT_ALL_QUERY_WITH_LIMIT, params, new PurchaseOrderCTPRowMapper() );
    }
}
