<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Clause;
use Illuminate\Http\JsonResponse;
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;
use App\Models\StatTree;
use App\Models\StatTreeLine;
use App\Models\StatTreeLineClause;

class ClauseController extends Controller
{

    public function list(){
        $clauses = Clause::all();
        return view('app.stat-tree.clauses.list', compact('clauses'));
    }

    public function dashboard(Clause $clause){
        return view('app.stat-tree.clauses.single', compact('clause'));
    }

    // view
    public function replaceAllPage(Request $request){
        return view('app.stat-tree.clauses.replace-all');
    }

    // process
    public function replaceAll(Request $request){

        $data = $request->get('data');
        
        $rows = json_decode($data);

        Clause::truncate();
        //Should we truncate related data?
        StatTree::truncate();
        StatTreeLine::truncate();
        StatTreeLineClause::truncate();

        for($i = 0; $i < count($rows); $i++){
            $row = $rows[$i];
            $clause = new Clause(); 

            $nextId = DB::select("select nextval('clause_id_seq')");
            $clause->id = $nextId[0]->nextval;
            $clause->uid = Uuid::uuid4();

            $clause->model = $row[0];
            $clause->question = $row[1];
            $clause->answer = $row[2];
            $clause->label = $row[1] . ($row[2] && $row[2] != '-' ? ' ' . $row[2] : '');
            $clause->clause_text = $row[3];
            $clause->position_index = $i;
            $clause->save();
        }
        
        return $this->pass();
    }

    // eps
    public function create(Request $request) {
        $clause = new Clause();
        $nextId = DB::select("select nextval('clause_id_seq')");
        $clause->id = $nextId[0]->nextval;
        $clause->uid = Uuid::uuid4();
        $clause->model = $request->input('model');
        $clause->question = $request->input('question');
        $clause->answer = $request->input('answer');
        $clause->label = $request->input('label');
        $clause->clause_text = $request->input('clauseText');
        $positionIndex = DB::select('select max(position_index) from clause');
        $clause->position_index = is_numeric($positionIndex[0]->max) ? $positionIndex[0]->max + 1 : 1;
        $clause->save();
        return $this->pass();
    }
    public function update(Request $request) {
        $clause = Clause::where('uid', $request->input('uid'))->first();
        if(!$clause) return $this->fail('Clause not found!');
        $clause->model = $request->input('model');
        $clause->question = $request->input('question');
        $clause->answer = $request->input('answer');
        $clause->label = $request->input('label');
        $clause->clause_text = $request->input('clauseText');
        $clause->save();
        return $this->pass();
    }
    public function remove(Request $request) {
        $clause = Clause::where('uid', $request->input('uid'))->first();
        if(!$clause) return $this->fail('Clause not found!');
        // TODO: allow only if this clause isn't used anywhere
        DB::select("delete from clause where id = {$clause->id}");
        return $this->pass();
    }

}
