<?php

namespace App\Services;

use Carbon\Carbon;
use App\Models\Surveillance;
use App\Models\SurveillanceCase;
use App\Models\CaseLabResult;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;

class DataRetrievalService
{

    public function getSurveillanceData()
    {
        try{
            $toDate = now();
            $this->getSARICases(now()->subDays(config('app.lookback_days.SARI'))->toDateString(), $toDate);  // done
            $this->getILICases(now()->subDays(config('app.lookback_days.ILI'))->toDateString(), $toDate);  // done
            $this->getLBMCases(now()->subDays(config('app.lookback_days.LBM'))->toDateString(), $toDate); // done
            $this->getAFICases(now()->subDays(config('app.lookback_days.AFI'))->toDateString(), $toDate); // done
            $this->getNSDCases(now()->subDays(config('app.lookback_days.NDS'))->toDateString(), $toDate);
            $this->getSEQCases(now()->subDays(config('app.lookback_days.SEQ'))->toDateString(), $toDate); // done
            Log::channel('jobs')->info($toDate->toDateString(). ' Service Reload Data Successfully Ran');
            return true;
        }
        catch (\Exception $e){
            Log::channel('jobs')->error($e->getMessage());
            return false;
        }

    }


    public function getSARICases($dateFrom, $dateTo=NULL)
    {
        $cond = "";
        $cond .= " and patient.patdate >= '".date('Y-m-d', strtotime($dateFrom))."' ";
        $cond .= (!empty($dateTo)) ? " and patient.patdate <='".date('Y-m-d', strtotime($dateTo))."' ":"";
        $sari_cases = DB::connection('mysql_nphl')
            ->select("
                       SELECT
                            patient.labcode,
                            min(if(year(tps.collected)=0,patient.patdate, tps.collected)) as patdate,
                            patient.isnewcase,
                            l.labname_en,
                            l.labaddress_en,
                            1 as surveillance_id,
                            min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,patient.patdate, tps.collected), 'Y')) as year_data,
                            min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,patient.patdate, tps.collected), 'W')) as week_data,
                            patient.patage,
                            patient.patsex,
                            NULL as is_alive,
                            p.proname_en
                        FROM niphc0_nphl.`labopatients` patient
                        inner join niphc0_nphl.sari_patients sr_p on sr_p.patid = patient.patid
                        inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid and tps.status = 1
                        inner join niphc0_nphl.laboratory l on l.labid = patient.patsource
                        left join niphc0_nphl.province p on p.proid = patient.frompro
                        WHERE patient.patgroup =2
                        and patient.status = 1
                        ".$cond . "
                        group by
                        patient.labcode,
                        patient.isnewcase,
                        l.labname_en,
                        l.labaddress_en,
                        patient.patage,
                        patient.patsex,
                        p.proname_en;"

            );

        $this->insert_surveillance_cases($sari_cases);


        $sari_case_results = DB::connection('mysql_nphl')->select("
                        SELECT
                            patient.labcode,
                            1 as surveillance_id,
                            if(trs.rtitle='Negatives', 0, 1) as is_positive,
                            if(trs.rtitle !='Negatives', concat('Influenza ',LEFT(UPPER(trs.rtitle),1)),'') pathogen_name,
                            if(trs.rtitle !='Negatives', trs.rtitle, '') as subtype,
                            'Influenza' as indicator
                        FROM `labopatients` patient
                        inner join sari_patients sr_p on sr_p.patid = patient.patid
                        inner join test_patsample tps on tps.patientid = patient.patid
                        inner join test_patsamtest tpst on tpst.patsmid = tps.sampleid and tpst.patid = tps.patientid
                        inner join test_pattestresult tptr on tptr.pstid = tpst.pstid
                        inner join test_resultselect trs on trs.rid = tptr.rsltid
                        WHERE patient.patgroup =2
                        and patient.status = 1
                        and tps.status = 1
                        and tpst.status = 1
                        and trs.status = 1
                        and tpst.labtestid in (775,216,239,238,212,220,340,381,382)
                        " .$cond. "
                        union

                        SELECT
                           patient.labcode,
                            1 as surveillance_id,
                            if(trs.rtitle='Negative', 0, 1) as is_positive,
                            if(trs.rtitle !='Negative', 'SARS-CoV-2', '') as pathogen_name,
                            '' as subtype,
                            'Covid-19' as indicator
                        FROM `labopatients` patient
                        inner join sari_patients sr_p
                        on sr_p.patid = patient.patid
                        inner join test_patsample tps
                        on tps.patientid = patient.patid
                        inner join test_patsamtest tpst
                        on tpst.patsmid = tps.sampleid
                        and tpst.patid = tps.patientid
                        inner join test_pattestresult tptr on tptr.pstid = tpst.pstid
                        inner join test_resultselect trs on trs.rid = tptr.rsltid
                        WHERE patient.patgroup =2
                        and patient.status = 1
                        and tps.status = 1
                        and tpst.status = 1
                        and trs.status = 1
                        and tpst.labtestid in (651,652,653,654,650)
                        and trs.rtitle !='Pending'
                        " .$cond);

        $this->insert_surveillance_case_lab_results($sari_case_results);

    }

    public function getILICases($dateFrom, $dateTo = NULL)
    {
        $cond = "";
        $cond .= " and patient.patdate >= '".date('Y-m-d', strtotime($dateFrom))."' ";
        $cond .= (!empty($dateTo)) ? " and patient.patdate <='".date('Y-m-d', strtotime($dateTo))."' ":"";
        $ili_cases = DB::connection('mysql_nphl')
            ->select("
                SELECT
                    patient.labcode,
                    min(if(year(tps.collected)=0,patient.patdate, tps.collected)) as patdate,
                    patient.isnewcase,
                    l.labname_en,
                    l.labaddress_en,
                    2 as surveillance_id,
                    min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,patient.patdate, tps.collected), 'Y')) as year_data,
                    min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,patient.patdate, tps.collected), 'W')) as week_data,
                    patient.patage,
                    patient.patsex,
                    NULL as is_alive,
                    p.proname_en
                FROM niphc0_nphl.`labopatients` patient
                inner join niphc0_nphl.ili_patients sr_p on sr_p.patid = patient.patid
                inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid and tps.status = 1
                inner join niphc0_nphl.laboratory l on l.labid = patient.patsource
                left join niphc0_nphl.province p on p.proid = patient.frompro
                WHERE patient.patgroup =3
                and patient.status = 1
                ".$cond . "
                group by
                patient.labcode,
                patient.isnewcase,
                l.labname_en,
                l.labaddress_en,
                patient.patage,
                patient.patsex,
                p.proname_en;"
            );


        $this->insert_surveillance_cases($ili_cases);

        $ili_case_results = DB::connection('mysql_nphl')
            ->select("
            SELECT
            distinct
            patient.labcode,
            2 as surveillance_id,
            if(trs.rtitle='Negatives', 0, 1) as is_positive,
            if(trs.rtitle !='Negatives', concat('Influenza ',LEFT(UPPER(trs.rtitle),1)),'') pathogen_name,
            if(trs.rtitle !='Negatives', trs.rtitle, '') as subtype,
            'Influenza' as indicator
            FROM niphc0_nphl.`labopatients` patient
            inner join niphc0_nphl.ili_patients sr_p on sr_p.patid = patient.patid
            inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid
            inner join niphc0_nphl.test_patsamtest tpst on tpst.patsmid = tps.sampleid and tpst.patid = tps.patientid
            inner join niphc0_nphl.test_pattestresult tptr on tptr.pstid = tpst.pstid
            inner join niphc0_nphl.test_resultselect trs on trs.rid = tptr.rsltid
            WHERE patient.patgroup = 3
            and patient.status = 1
            and tps.status = 1
            and tpst.status = 1
            and trs.status = 1
            and tpst.labtestid in (775,216,239,238,212,220,340,381,382)
            ".$cond."

            union

            SELECT
            DISTINCT
            patient.labcode,
            2 as surveillance_id,
            if(trs.rtitle='Negative', 0, 1) as is_positive,
            if(trs.rtitle !='Negative', 'SARS-CoV-2', '') as pathogen_name,
            '' as subtype,
            'Covid-19' as indicator
            FROM niphc0_nphl.`labopatients` patient
            inner join niphc0_nphl.ili_patients sr_p on sr_p.patid = patient.patid
            inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid
            inner join niphc0_nphl.test_patsamtest tpst on tpst.patsmid = tps.sampleid and tpst.patid = tps.patientid
            inner join niphc0_nphl.test_pattestresult tptr on tptr.pstid = tpst.pstid
            inner join niphc0_nphl.test_resultselect trs on trs.rid = tptr.rsltid
            WHERE patient.patgroup = 3
            and patient.status = 1
            and tps.status = 1
            and tpst.status = 1
            and trs.status = 1
            and tpst.labtestid in (651,652,653,654,650)
            and trs.rtitle !='Pending'
            ".$cond);

        $this->insert_surveillance_case_lab_results($ili_case_results);

    }

    public function getLBMCases($dateFrom, $dateTo = NULL)
    {
        $cond = "";
        $cond .= " and patient.patdate >= '".date('Y-m-d', strtotime($dateFrom))."' ";
        $cond .= (!empty($dateTo)) ? " and patient.patdate <='".date('Y-m-d', strtotime($dateTo))."' ":"";
        $lbm_cases = DB::connection('mysql_nphl')
            ->select("
                SELECT
                    patient.labcode,
                    min(if(year(tps.collected)=0,patient.patdate, tps.collected)) as patdate,
                    patient.isnewcase,
                    l.labname_en,
                    l.labaddress_en,
                    3 as surveillance_id,
                    min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,patient.patdate, tps.collected), 'Y')) as year_data,
                    min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,patient.patdate, tps.collected), 'W')) as week_data,
                    patient.patage,
                    patient.patsex,
                    NULL as is_alive,
                    p.proname_en
                FROM niphc0_nphl.`labopatients` patient
                inner join niphc0_nphl.lbms_patients on lbms_patients.patid = patient.patid
                inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid and tps.status = 1
                left join niphc0_nphl.province p on p.proid = patient.frompro
                inner join niphc0_nphl.laboratory l on l.labid = patient.patsource
                WHERE patient.patgroup = 17
                and patient.status = 1
                ".$cond . "
                group by
                patient.labcode,
                patient.isnewcase,
                l.labname_en,
                l.labaddress_en,
                patient.patage,
                patient.patsex,
                p.proname_en;"
            );

        $this->insert_surveillance_cases($lbm_cases);

        $lbm_case_results = DB::connection('mysql_nphl')
            ->select("
                SELECT
                distinct
                patient.labcode,
                3 as surveillance_id,
                if(trs.rtitle='Negatives', 0, 1) as is_positive,
                if(trs.rtitle !='Negatives', concat('Influenza ',LEFT(UPPER(trs.rtitle),1)),'') pathogen_name,
                if(trs.rtitle !='Negatives', trs.rtitle, '') as subtype,
                'Influenza' as indicator
                FROM niphc0_nphl.`labopatients` patient
                inner join niphc0_nphl.lbms_patients sr_p on sr_p.patid = patient.patid
                inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid
                inner join niphc0_nphl.test_patsamtest tpst on tpst.patsmid = tps.sampleid and tpst.patid = tps.patientid
                inner join niphc0_nphl.test_pattestresult tptr on tptr.pstid = tpst.pstid
                inner join niphc0_nphl.test_resultselect trs on trs.rid = tptr.rsltid
                WHERE patient.patgroup = 17
                and patient.status = 1
                and tps.status = 1
                and tpst.status = 1
                and trs.status = 1
                and tpst.labtestid in (775,216,239,238,212,220,340,381,382)
                ".$cond."
                union

                SELECT
                DISTINCT
                patient.labcode,
                3 as surveillance_id,
                if(trs.rtitle='Negative', 0, 1) as is_positive,
                if(trs.rtitle !='Negative', 'SARS-CoV-2', '') as pathogen_name,
                '' as subtype,
                'Covid-19' as indicator
                FROM niphc0_nphl.`labopatients` patient
                inner join niphc0_nphl.lbms_patients sr_p on sr_p.patid = patient.patid
                inner join niphc0_nphl.test_patsample tps on tps.patientid = patient.patid
                inner join niphc0_nphl.test_patsamtest tpst on tpst.patsmid = tps.sampleid and tpst.patid = tps.patientid
                inner join niphc0_nphl.test_pattestresult tptr on tptr.pstid = tpst.pstid
                inner join niphc0_nphl.test_resultselect trs on trs.rid = tptr.rsltid
                WHERE patient.patgroup = 17
                and patient.status = 1
                and tps.status = 1
                and tpst.status = 1
                and trs.status = 1
                and tpst.labtestid in (651,652,653,654,650)
                and trs.rtitle !='Pending'
                ".$cond);

        $this->insert_surveillance_case_lab_results($lbm_case_results);

    }

    public function getAFICases($dateFrom, $dateTo = NULL)
    {
        $cond = "";
        $cond .= " and afi_case.date_of_interview >= '".date('Y-m-d', strtotime($dateFrom))."' ";
        $cond .= (!empty($dateTo)) ? " and afi_case.date_of_interview <='".date('Y-m-d', strtotime($dateTo))."' ":"";
        $afi_cases = DB::connection('mysql_afi')
            ->select("
                SELECT
                    afi_lab_result.sample_code as labcode,
                    afi_case.date_of_interview as patdate,
                    1 as isnewcase,
                    afi_case.sentinel_site as labname_en,
                    '' as labaddress_en,
                    4 as surveillance_id,
                    niphc0_nphl.get_epi_period(afi_case.date_of_interview, 'Y') as year_data,
                    niphc0_nphl.get_epi_period(afi_case.date_of_interview, 'W') as week_data,
                    ifnull(DATEDIFF(afi_case.date_of_interview, date_of_birth),180) as patage,
                    if(afi_case.sex='Male','M','F') as patsex,
                    NULL as is_alive,
                    p.proname_en as proname_en
                FROM afi_db.`afi_tbl_afiform` as afi_case
                left join afi_db.afi_tbl_result afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                left join niphc0_nphl.province p on p.proid = afi_case.residence_province
                where
                afi_case.afi_event is NOT null
                and afi_case.date_of_interview is not null
                and afi_lab_result.sample_code is not null " .$cond);

        $this->insert_surveillance_cases($afi_cases);

        $afi_lab_results = DB::connection('mysql_afi')
            ->select(
                "SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.influenza_result='Positive',1,0) as is_positive,
                        if(afi_lab_result.influenza_result='Positive', concat('Influenza ',LEFT(UPPER(JSON_UNQUOTE(JSON_EXTRACT(afi_lab_result.influenza_subtypes, '$[0].id'))),1)),'') pathogen_name,
                        if(afi_lab_result.influenza_result='Positive', REPLACE(UPPER(JSON_UNQUOTE(JSON_EXTRACT(afi_lab_result.influenza_subtypes, '$[0].id'))),'_','/'),'') as subtype,
                        'Influenza' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.influenza_result in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond."

                        union

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.sarscov2_result='Positive',1,0) as is_positive,
                        if(afi_lab_result.sarscov2_result='Positive', 'SARS-CoV-2','') pathogen_name,
                        '' as subtype,
                        'Covid-19' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.sarscov2_result in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null
                        ".$cond."

                        union

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.dengue_pcr='Positive',1,0) as is_positive,
                        if(afi_lab_result.dengue_pcr='Positive', 'Dengue Virus','') pathogen_name,
                        '' as subtype,
                        'Dengue Virus|PCR' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.dengue_pcr in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond."

                        union

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.chikungunya_pcr='Positive',1,0) as is_positive,
                        if(afi_lab_result.chikungunya_pcr='Positive', 'Chikungunya Virus','') pathogen_name,
                        '' as subtype,
                        'Chikungunya Virus|PCR' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.chikungunya_pcr in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond."

                        union

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.leptospira_pcr='Positive',1,0) as is_positive,
                        if(afi_lab_result.leptospira_pcr='Positive', 'Leptospira spp.','') pathogen_name,
                        '' as subtype,
                        'Leptospira spp.|PCR' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.leptospira_pcr in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond."

                        union

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.plasmodium_pcr='Positive',1,0) as is_positive,
                        if(afi_lab_result.plasmodium_pcr='Positive', 'Plasmodium spp.','') pathogen_name,
                        '' as subtype,
                        'Plasmodium spp.|PCR' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.plasmodium_pcr in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond."

                        UNION

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.rickettsia_pcr='Positive',1,0) as is_positive,
                        if(afi_lab_result.rickettsia_pcr='Positive', 'Rickettsia spp.','') pathogen_name,
                        '' as subtype,
                        'Rickettsia spp.|PCR' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.rickettsia_pcr in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond."

                        UNION

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.salmonella_pcr='Positive',1,0) as is_positive,
                        if(afi_lab_result.salmonella_pcr='Positive', 'Salmonella spp.','') pathogen_name,
                        '' as subtype,
                        'Salmonella spp.|PCR' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.salmonella_pcr in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond."

                        UNION

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.westnile_pcr='Positive',1,0) as is_positive,
                        if(afi_lab_result.westnile_pcr='Positive', 'West Nile Virus','') pathogen_name,
                        '' as subtype,
                        'West Nile Virus|PCR' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.westnile_pcr in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond."

                        UNION

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.zika_pcr='Positive',1,0) as is_positive,
                        if(afi_lab_result.zika_pcr='Positive', 'ZIKA Virus','') pathogen_name,
                        '' as subtype,
                        'ZIKA Virus|PCR' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.zika_pcr in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond."

                        UNION

                        # SERUM

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.chikungunya_serum='Positive',1,0) as is_positive,
                        if(afi_lab_result.chikungunya_serum='Positive', 'Chikungunya Virus','') pathogen_name,
                        '' as subtype,
                        'Chikungunya Virus|Serum' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.chikungunya_serum in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond."

                        UNION

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.dengue_serum='Positive',1,0) as is_positive,
                        if(afi_lab_result.dengue_serum='Positive', 'Dengue Virus','') pathogen_name,
                        '' as subtype,
                        'Dengue Virus|Serum' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.dengue_serum in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond."

                        UNION

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.westnile_serum='Positive',1,0) as is_positive,
                        if(afi_lab_result.westnile_serum='Positive', 'West Nile Virus','') pathogen_name,
                        '' as subtype,
                        'West Nile Virus|Serum' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.westnile_serum in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond."

                        UNION

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.zika_serum='Positive',1,0) as is_positive,
                        if(afi_lab_result.zika_serum='Positive', 'ZIKA Virus','') pathogen_name,
                        '' as subtype,
                        'ZIKA Virus|Serum' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.zika_serum in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond."

                        UNION

                        SELECT
                        afi_lab_result.sample_code as labcode,
                        4 as surveillance_id,
                        if(afi_lab_result.je_virus='Positive',1,0) as is_positive,
                        if(afi_lab_result.je_virus='Positive', 'Japanese Encephalitis Virus','') pathogen_name,
                        '' as subtype,
                        'Japanese Encephalitis Virus|Serum' as indicator
                        FROM afi_db.`afi_tbl_afiform` as afi_case
                        INNER join afi_db.afi_tbl_result as afi_lab_result on afi_lab_result.unique_afi_id = afi_case.unique_afi_id
                        where afi_lab_result.sample_code is not null
                        and afi_lab_result.je_virus in('Negative', 'Positive')
                        and afi_case.afi_event is NOT null
                        and afi_case.date_of_interview is not null
                        and afi_lab_result.sample_code is not null ".$cond
            );

        $this->insert_surveillance_case_lab_results($afi_lab_results);
    }

    public function getNSDCases($dateFrom, $dateTo = NULL)
    {
        $cond = "";
        $cond .= " and sc.sc_date >= '".date('Y-m-d', strtotime($dateFrom))."' ";
        $cond .= (!empty($dateTo)) ? " and sc.sc_date <='".date('Y-m-d', strtotime($dateTo))."' ":"";
        $nds_cases = DB::connection('mysql_nds')
            ->select("
                SELECT
                    DISTINCT
                    sc.sc_sample_code as labcode,
                    sc.sc_date as patdate,
                    1 as isnewcase,
                    site_ad.sn_name as labname_en,
                    '' as labaddress_en,
                    5 as surveillance_id,
                    niphc0_nphl.get_epi_period(sc.sc_date, 'Y') as year_data,
                    niphc0_nphl.get_epi_period(sc.sc_date, 'W') as week_data,
                    ifnull(DATEDIFF(sc.sc_date, patient.pat_dob),180) as patage,
                    if(patient.pat_gender='Male','M','F') as patsex,
                    NULL as is_alive,
                    pro.pro_eng as proname_en
                from nds_db.tbl_nds_site_invest case_inv
                inner join nds_db.tbl_nds_siteinvet_patient inv_patient
                on inv_patient.pksi_id = case_inv.pksi_id
                inner join nds_db.tbl_nds_site_address site_ad
                on site_ad.pksn_id = case_inv.fksn_id
                inner join nds_db.tbl_nds_patient patient
                on patient.pkpat_id = inv_patient.fkpat_id
                inner join nds_db.tbl_nds_basic_sample bs
                on bs.fkpat_id = inv_patient.fkpat_id
                inner join nds_db.tbl_nds_sample_collection sc
                on sc.fkbs_id = bs.pkbs_id
                left join nds_db.tblprovince pro
                on pro.pkpro_code = JSON_UNQUOTE(JSON_EXTRACT(patient.pat_address, '$[0].province'))
                where
                sc.sc_sample_code is not null
                and length(sc_sample_code)>0 " .$cond);

        $this->insert_surveillance_cases($nds_cases);

        $nds_lab_results = DB::connection('mysql_nds')
            ->select("
                SELECT
                    DISTINCT
                    sc.sc_sample_code as labcode,
                    5 as surveillance_id,
                    rt.rt_positive as is_positive,
                    trt.trt_name pathogen_name,
                    '' as subtype,
                    trt.trt_name as indicator
                from nds_db.tbl_nds_site_invest case_inv
                inner join nds_db.tbl_nds_siteinvet_patient inv_patient
                on inv_patient.pksi_id = case_inv.pksi_id
                inner join nds_db.tbl_nds_patient patient
                on patient.pkpat_id = inv_patient.fkpat_id
                inner join nds_db.tbl_nds_basic_sample bs
                on bs.fkpat_id = inv_patient.fkpat_id
                inner join nds_db.tbl_nds_sample_collection sc
                on sc.fkbs_id = bs.pkbs_id
                inner join nds_db.tbl_nds_result_test rt
                on rt.fkpat_id = bs.fkpat_id
                inner join nds_db.tbl_nds_test_result_type trt
                on trt.pktrt_id = rt.fktrt_id

                where
                sc.sc_sample_code is not null
                and length(sc_sample_code)>0 " .$cond);

        $this->insert_surveillance_case_lab_results($nds_lab_results);

    }

    public function getSEQCases($dateFrom, $dateTo = NULL)
    {
        $cond = "";
        $cond .= " and c.patdate >= '".date('Y-m-d', strtotime($dateFrom))."' ";
        $cond .= (!empty($dateTo)) ? " and c.patdate <='".date('Y-m-d', strtotime($dateTo))."' ":"";
        $seq_cases = DB::connection('mysql_nphl')
            ->select("
            SELECT DISTINCT
                c.labcode,
                min(if(year(tps.collected)=0,c.patdate, tps.collected)) as patdate,
                c.isnewcase,
                l.labname_en,
                l.labaddress_en,
                6 as surveillance_id,
                min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,c.patdate, tps.collected), 'Y')) as year_data,
                min(niphc0_nphl.get_epi_period(if(year(tps.collected)=0,c.patdate, tps.collected), 'W')) as week_data,
                c.patage,
                c.patsex,
                NULL as is_alive,
                pro.proname_en
                from niphc0_nphl.sq_batch sqb
                inner join niphc0_nphl.sq_sub_batch sqsb on sqsb.bt_id = sqb.bt_id
                inner join niphc0_nphl.labopatients c on c.patid = sqsb.patid
                inner join niphc0_nphl.laboratory l on l.labid = p.patsource
                inner join niphc0_nphl.test_patsample tps on tps.patientid = c.patid and tps.status = 1
                inner join niphc0_nphl.test_patsamtest tpst on tpst.patsmid = tps.sampleid and tpst.patid = tps.patientid
                left join niphc0_nphl.province pro on pro.proid = c.frompro
            where c.status = 1
            and tpst.labtestid IN ( 659, 662, 666, 667, 668 )
            " . $cond . "
            group by
            c.labcode,
            c.isnewcase,
            l.labname_en,
            l.labaddress_en,
            c.patage,
            c.patsex,
            pro.proname_en;"
            );

        $this->insert_surveillance_cases($seq_cases);

        $seq_lab_results = DB::connection('mysql_nphl')
            ->select("
                SELECT
                   labcode,
                   6 as surveillance_id,
                   1 is_positive,
                   ifnull(seqlineage,'N/A') as pathogen_name,
                   GROUP_CONCAT(seq_result SEPARATOR '') as subtype,
                   (case
                    when (Group_concat(influ_result SEPARATOR '')='Negatives' or Group_concat(influ_result SEPARATOR '')='') and Group_concat(covid_result SEPARATOR '')='Positive' then 'Covid-19'
                    when (Group_concat(influ_result SEPARATOR '') !='Negatives' and Group_concat(influ_result SEPARATOR '')!='') and Group_concat(covid_result SEPARATOR '')='Negative' then 'Influenza'
                    when (Group_concat(influ_result SEPARATOR '') !='Negatives' and Group_concat(influ_result SEPARATOR '')!='') and Group_concat(covid_result SEPARATOR '')='Positive' then 'Co-infection'
                    else 'N/A'
                    end) as indicator
            FROM   (SELECT
                           c.labcode,
                           ( CASE
                               WHEN e1.labtestid IN ( 238, 340, 381 ) THEN e3.rtitle
                               ELSE ''
                             end )    influ_result,
                           ( CASE
                               WHEN e1.labtestid IN ( 650, 651 ) THEN e3.rtitle
                               ELSE ''
                             end )    covid_result,
                           ( CASE
                               WHEN e1.labtestid IN ( 668, 659, 666, 667, 662 ) THEN
                               e3.rtitle
                               ELSE ''
                             end )    seq_result,
                           b.seqlineage
                    FROM   niphc0_nphl.sq_batch a
                           LEFT JOIN niphc0_nphl.sq_sub_batch b
                                  ON a.bt_id = b.bt_id
                           LEFT JOIN niphc0_nphl.labopatients c
                                  ON b.patid = c.patid
                           LEFT JOIN niphc0_nphl.laboratory d
                                  ON c.patsource = d.labid
                           LEFT JOIN niphc0_nphl.test_patsamtest e1
                                  ON c.patid = e1.patid
                           LEFT JOIN niphc0_nphl.test_pattestresult e2
                                  ON e1.pstid = e2.pstid
                           LEFT JOIN niphc0_nphl.test_resultselect e3
                                  ON e2.rsltid = e3.rid
                           LEFT JOIN niphc0_nphl.ncov_patients e4
                                  ON c.patid = e4.patid
                           LEFT JOIN niphc0_nphl.test_patsample e5
                                  ON c.patid = e5.patientid
                                     AND e5.sampleid = e1.patsmid
                           LEFT JOIN niphc0_nphl.test_patsmpbyunit f
                                  ON e5.sampleid = f.patsamid
                                     AND c.patid = f.patid
                    WHERE e1.labtestid IN ( 238, 340, 381, 650,
                                                 651, 668, 659, 666,
                                                 667, 662 )
                                                 " . $cond . "
                   ) tbl_a
            GROUP  BY labcode,
                      seqlineage
                      HAVING
                GROUP_CONCAT(seq_result SEPARATOR '') not in ('','Inconclusive', 'In Progress')
            ");

        $this->insert_surveillance_case_lab_results($seq_lab_results);

    }

    private function insert_surveillance_cases($cases){

        $case_data = [];
        foreach ($cases as $case) {
            $case_data[] = [
                'lab_code'          => $case->labcode,
                'case_date'          => $case->patdate,
                'is_newcase'        => $case->isnewcase,
                'sentinel_site_name'       => $case->labname_en,
                'site_province_name'    => $case->labaddress_en,
                'surveillance_id'  => $case->surveillance_id,
                'year_data'        => $case->year_data,
                'week_data'        => $case->week_data,
                'patient_age_inday'           => $case->patage,
                'patient_sex'           => $case->patsex,
                'is_alive'         => $case->is_alive,
                'patient_province'       => $case->proname_en
            ];
        }

        $case_chunks = array_chunk($case_data, 100);
        foreach ($case_chunks as $chunk) {
            DB::connection('mysql')->table('tmp_surveillance_cases')->upsert($chunk, [
                'lab_code',
                'surveillance_id'
            ], [
                'case_date',
                'is_newcase',
                'sentinel_site_name',
                'site_province_name',
                'year_data',
                'week_data',
                'patient_age_inday',
                'patient_sex',
                'is_alive',
                'patient_province'
            ]);
        }
    }

    private function insert_surveillance_case_lab_results($lab_results){
        $result_data = [];
        foreach ($lab_results as $lab_result) {
            $result_data[] = [
                'lab_code'          => $lab_result->labcode,
                'surveillance_id'  => $lab_result->surveillance_id,
                'is_positive'        => $lab_result->is_positive,
                'pathogen_name'        => $lab_result->pathogen_name,
                'subtype'           => $lab_result->subtype,
                'indicator'           => $lab_result->indicator
            ];
        }
        $result_chunks = array_chunk($result_data, 100);
        foreach ($result_chunks as $chunk) {
            DB::connection('mysql')->table('tmp_case_lab_results')->upsert($chunk,
                [
                    'lab_code',
                    'surveillance_id',
                    'indicator'
                ],
                [
                    'is_positive',
                    'pathogen_name',
                    'subtype'
                ]);
        }
    }

}
