<?php

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class MeasurementSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        // Device IDs and IMEIs
        // BP - 1 - 987987987983
        // WEIGHT - 2 - 987987987984

        $clientID = 1;
        $bpDeviceID = 1;
        $bpDeviceIMEI = '987987987983';
        $weightDeviceID = 2;
        $weightDeviceIMEI = '987987987984';

        // delete all
        DB::table('bdt_measurement')->truncate();
        DB::table('client_bdt_measurement')->truncate();
        DB::table('measurement')->truncate();

        // create dates for the last 90 days
        $dates = [];
        for ($i=90; $i>=0; $i--) {
            $d = date_sub(date_create(), date_interval_create_from_date_string($i . " day" . ($i === 1 ? '' : 's')));
            $dates[] = date_format($d, "Y-m-d");
        }

        $bdt_measurement_next_id = 1;
        $client_bdt_measurement_next_id = 1;
        $measurement_next_id = 1;

        // for each date, create measurements
        for ($i=0; $i<count($dates); $i++) {

            $date = $dates[$i];

            // -- BP -----------------------------------------------------------

            $sbp = rand(130, 145);
            $dbp = rand(75, 95);
            $pulse = rand(60, 70);

            // create bdt_measurement
            $bdt_measurement_id = DB::table('bdt_measurement')->insertGetId([
                'id' => $bdt_measurement_next_id++,
                'uid' => Str::uuid(),
                'imei' => $bpDeviceIMEI,
                'ts' => strtotime($date),
                'battery_voltage' => '12',
                'signal_strength' => '100',
                'value_tare' => null,
                'value_weight' => null,
                'weight_in_pounds' => null,
                'value_systolic' => round($sbp / 0.0075006),
                'value_diastolic' => round($dbp / 0.0075006),
                'value_pulse' => $pulse,
                'value_unit' => null,
                'value_irregular' => null,
                'rssi' => null,
                'device_id' => null,
                'device_category' => 'BP',
                'created_at' => null,
                'created_by_session_id' => null,
                'type' => 'BDTMeasurement',
                'diastolic_bp_in_mm_hg' => $sbp,
                'systolic_bp_in_mm_hg' => $dbp,
            ]);

            // create client_bdt_measurement
            $client_bdt_measurement_id = DB::table('client_bdt_measurement')->insertGetId([
                'id' => $client_bdt_measurement_next_id++,
                'uid' => Str::uuid(),
                'client_id' => $clientID,
                'bdt_measurement_id' => $bdt_measurement_id,
                'status' => null,
                'status_memo' => null,
                'created_at' => null,
                'created_by_session_id' => null,
                'type' => 'ClientBDTMeasurement',
                'measurement_id' => null, // to update at the end
            ]);

            // create measurement (1 parent and 2 children for BP)
            // parent BP measurement
            $parent_measurement_id = DB::table('measurement')->insertGetId([
                'id' => $measurement_next_id++,
                'created_at' => date('Y-m-d'),
                'type' => 'Measurement',
                'uid' => Str::uuid(),
                'is_removed' => false,
                'removal_memo' => null,
                'removed_at' => null,
                'effective_date' => $date,
                'label' => 'BP',
                'memo' => null,
                'value' => "$sbp/$dbp mmHg",
                'created_by_session_id' => null,
                'removed_by_session_id' => null,
                'client_id' => 1,
                'status' => null,
                'status_memo' => null,
                'numeric_value' => null,
                'client_bdt_measurement_id' => $client_bdt_measurement_id,
                'parent_measurement_id' => null,
                'source' => 'Device',
            ]);
            // child SBP measurement
            $sbp_measurement_id = DB::table('measurement')->insertGetId([
                'id' => $measurement_next_id++,
                'created_at' => date('Y-m-d'),
                'type' => 'Measurement',
                'uid' => Str::uuid(),
                'is_removed' => false,
                'removal_memo' => null,
                'removed_at' => null,
                'effective_date' => $date,
                'label' => 'SBP',
                'memo' => null,
                'value' => $sbp,
                'created_by_session_id' => null,
                'removed_by_session_id' => null,
                'client_id' => 1,
                'status' => null,
                'status_memo' => null,
                'numeric_value' => $sbp,
                'client_bdt_measurement_id' => $client_bdt_measurement_id,
                'parent_measurement_id' => $parent_measurement_id,
                'source' => 'Device',
            ]);
            // child DBP measurement
            $dbp_measurement_id = DB::table('measurement')->insertGetId([
                'id' => $measurement_next_id++,
                'created_at' => date('Y-m-d'),
                'type' => 'Measurement',
                'uid' => Str::uuid(),
                'is_removed' => false,
                'removal_memo' => null,
                'removed_at' => null,
                'effective_date' => $date,
                'label' => 'DBP',
                'memo' => null,
                'value' => $dbp,
                'created_by_session_id' => null,
                'removed_by_session_id' => null,
                'client_id' => 1,
                'status' => null,
                'status_memo' => null,
                'numeric_value' => $dbp,
                'client_bdt_measurement_id' => $client_bdt_measurement_id,
                'parent_measurement_id' => $parent_measurement_id,
                'source' => 'Device',
            ]);

            // set measurement_id on client_bdt_measurement
            DB::table('client_bdt_measurement')
                ->where('id', $client_bdt_measurement_id)
                ->update([
                    'measurement_id' => $parent_measurement_id
                ]);

            // -- WEIGHT -----------------------------------------------------------

            $weight = rand(120, 145);

            // create bdt_measurement
            $bdt_measurement_id = DB::table('bdt_measurement')->insertGetId([
                'id' => $bdt_measurement_next_id++,
                'uid' => Str::uuid(),
                'imei' => $weightDeviceIMEI,
                'ts' => strtotime($date),
                'battery_voltage' => '12',
                'signal_strength' => '100',
                'value_tare' => null,
                'value_weight' => round($weight / 0.00220462),
                'weight_in_pounds' => $weight,
                'value_systolic' => null,
                'value_diastolic' => null,
                'value_pulse' => null,
                'value_unit' => null,
                'value_irregular' => null,
                'rssi' => null,
                'device_id' => null,
                'device_category' => 'WEIGHT',
                'created_at' => null,
                'created_by_session_id' => null,
                'type' => 'BDTMeasurement',
                'diastolic_bp_in_mm_hg' => null,
                'systolic_bp_in_mm_hg' => null,
            ]);

            // create client_bdt_measurement
            $client_bdt_measurement_id = DB::table('client_bdt_measurement')->insertGetId([
                'id' => $client_bdt_measurement_next_id++,
                'uid' => Str::uuid(),
                'client_id' => $clientID,
                'bdt_measurement_id' => $bdt_measurement_id,
                'status' => null,
                'status_memo' => null,
                'created_at' => null,
                'created_by_session_id' => null,
                'type' => 'ClientBDTMeasurement',
                'measurement_id' => null, // to update at the end
            ]);

            // create measurement (1 parent only)
            $parent_measurement_id = DB::table('measurement')->insertGetId([
                'id' => $measurement_next_id++,
                'created_at' => date('Y-m-d'),
                'type' => 'Measurement',
                'uid' => Str::uuid(),
                'is_removed' => false,
                'removal_memo' => null,
                'removed_at' => null,
                'effective_date' => $date,
                'label' => 'WEIGHT',
                'memo' => null,
                'value' => "$weight lbs",
                'created_by_session_id' => null,
                'removed_by_session_id' => null,
                'client_id' => 1,
                'status' => null,
                'status_memo' => null,
                'numeric_value' => $weight,
                'client_bdt_measurement_id' => $client_bdt_measurement_id,
                'parent_measurement_id' => null,
                'source' => 'Device',
            ]);

            // set measurement_id on client_bdt_measurement
            DB::table('client_bdt_measurement')
                ->where('id', $client_bdt_measurement_id)
                ->update([
                    'measurement_id' => $parent_measurement_id
                ]);

        }

    }
}
