package it.integry.integrywmsnative.core.data_store.db.dao;

import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;

import java.util.Date;
import java.util.List;

import it.integry.integrywmsnative.core.data_store.db.entity.SqlMtbColt;
import it.integry.integrywmsnative.core.data_store.db.wrappers.DocInternoWrapper;
 
@Dao
public interface MtbColtDao {
    @Query("SELECT * from mtb_colt")
    List<SqlMtbColt> getAll();

    @Insert
    Long insert(SqlMtbColt mtbColt);

    @Update
    void update(SqlMtbColt mtbColt);

    @Delete
    void delete(SqlMtbColt mtbColt);

    @Query("SELECT mtb_colt.*, count(mtb_colr.id) as countArticoli from mtb_colt left outer join mtb_colr on (mtb_colr.id_collo = mtb_colt.id) where" +
            " cod_dtip_provv = :codDtip " +
            " AND ( cod_anag = :codAnag OR (cod_anag is null and :codAnag is null)) " +
            " AND ( cod_vdes = :codVdes OR (cod_vdes is null and :codVdes is null)) " +
            " AND ( mtb_colt.data_doc = :dataDoc OR (mtb_colt.data_doc is null and :dataDoc is null)) " +
            " AND ( mtb_colt.num_doc = :numDoc OR (mtb_colt.num_doc is null and :numDoc is null)) " +
            " and cod_dtip is null group by mtb_colt.id")
    List<DocInternoWrapper> getLocalDocumentsByCodDtip(String codDtip, String codAnag, String codVdes, Date dataDoc, String numDoc);

    @Query("SELECT ifnull(MAX(num_collo),0) +1 as num_collo from mtb_colt")
    Integer getNextNumCollo();
}
