<?php

namespace App\Http\Controllers\Api\RandDVoting;

use App\Http\Controllers\Controller;
use App\Models\RandDVoting;
use Illuminate\Http\Request;
use App\Models\NewRandD;
use App\Models\RandDVotingPaperAnswer;
use Illuminate\Support\Facades\Hash;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;
use Illuminate\Support\Facades\Auth;
use App\Models\User;

class AdminVoterController extends Controller
{
    public function startVoting($id)
    {
        if (auth()->user()) {
            auth()->user()->addActivityComplete(1063);
        }
        if (RandDVoting::where('project_id', $id)->where('status', '2')->first()) {
            return response()->json([
                'status' => 'failed',
                'message' => 'this voting was starting'
            ]);
        }

        $create = RandDVoting::create([
            'project_id' => $id,
            'description' => null,
            'status' => '2',
            'user_id' => Auth::user()->id
        ]);
        
        return response()->json([
            'message' => 'success',
            'data' => $create,
            'url' => 'https://rms.rmto.ir/randd/voting/' . $create->id,
        ]);
    }

    public function endVoting($id)
    {
        if (auth()->user()) {
            auth()->user()->addActivityComplete(1064);
        }
        if (RandDVoting::where('project_id' ,$id)->where('status', '!=' , '2')->first()) {
            return response()->json([
                'status' => 'failed',
                'message' => 'you cant access this route!'
            ]);
        }

        $vote = RandDVoting::where('project_id' ,$id)
                            ->where('status', '2')
                            ->update([
                                'status' => '4'
                            ]);
        NewRandD::find($id)
                ->update([
                    'project_status' => '2'
                ]);
                            
        return response()->json([
            'message' => 'success'
        ]);
    }

    public function showResult($id)
    {
        $vote = RandDVoting::find($id);

        // if vote has not finished, result is 403 forbidden
        if ($vote->status != '4') {
            abort(403);
        }
        /// get users data
        $participants = [];
        foreach ($vote->answers as $answer) {
            $participants[] = $answer->user;
        }

        // General voting information
        $data = [
            'project_name' => $vote->project->title,
            'description' => $vote->description,
            'status' => $vote->status,
            'participants_number' => count($vote->answers),
            'participants' => $participants,
            'voterـstarter' => $vote->user->first_name . ' ' . $vote->user->last_name,
            'average' => $vote->answers->sum('score') / count($vote->answers),
        ];

        return response()->json([
            'message' => 'success',
            'data' => $data
        ]);
    }
}