<?php

namespace app\modules\meeting\essentials;

use yii\base\Model;
use app\modules\meeting\models\db\IstMeetingMember;
use app\modules\meeting\models\db\IstMeeting;
use app\modules\meeting\models\db\IstTableSit;

/**
 * Class that handles all meeting management functionality
 */
class Manage
{
    const MID_LEN = 6;

    public function progress(Model $model){


        // Validate model
        if (!$model->validate()){
            return false;
        }

        // Get table sit record
        if (($sit = IstTableSit::find()->where(['sid' => $model->sit_id])->one()) == null){
            return false;
        }

        // Get table
        if (($table = $sit->getTable()->one()) == null){
            return false; 
        }

        // Find out if $table has a meeting 
        if (($meeting = IstMeeting::findOne(['table_id' => $table->id, 'status' => 'active'])) == null){
            return null;
        }

        // Get meeting members
        $meeeting_members = $meeting->getIstMeetingMembers();

        return $meeting->attributes;
    }

    public function start(Model $model){

        $newMeeting = new IstMeeting();
        $newMeetingMember = new IstMeetingMember();

        // Validate model
        if (!$model->validate()){
            return false;
        }
        
        // Get table sit record
        if (($sit = IstTableSit::find()->where(['sid' => $model->sit_id])->one()) == null){
            return false;
        }

        // Get table
        if (($table = $sit->getTable()->one()) == null){
            return false; 
        }

        // Write new meeting and new meeting member records
        $transaction = \Yii::$app->db->beginTransaction();
        try{
            // New meeting
            $newMeeting->mid = $this->newMid();
            $newMeeting->table_id = $table->id;
            $newMeeting->status = 'active';
            $newMeeting->start_datetime = @date("Y-m-d H:i:s");

            if (!$newMeeting->save()){
                return false;
            }

            // New meeting member
            $newMeetingMember->sit_id = $sit->id;
            $newMeetingMember->meeting_id = $newMeeting->id;
            $newMeetingMember->type = 'initiator';
            $newMeetingMember->ip = $model->device_ip;
            $newMeetingMember->start_datetime = @date("Y-m-d H:i:s");

            if (!$newMeetingMember->save()){
                $transaction->rollBack();
                return false;
            }

            $transaction->commit();

            $meeting_members = $newMeeting->getIstMeetingMembers()->all();

            $meeting = $newMeeting->attributes;
            $meeting['members'] = [];

            // Attach members
            foreach($meeting_members as $idx => $record){
                array_push($meeting['members'], $record->attributes);
            }

            return $meeting;
        }
        catch(\Exception $e){
            $transaction->rollBack();
            return false;
        }
    }

    public function details(){

    }


    public function newMid(){
        $mid = "";
        $rnd_sample = hash('sha512', (random_bytes(Manage::MID_LEN)));

        // Pick MID_LEN characters for id
        for ($i = 0; $i < Manage::MID_LEN; $i++){
            $mid .= $rnd_sample[rand(0, (strlen($rnd_sample)-1))];
        }

        return strtoupper($mid);
    }
}

