<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Link;

class LinkController extends Controller
{
    public function add(Request $request)
    {
        $this->validate($request, [
            'source' => 'required',
            'target' => 'required',
            'type' => 'required',
            'version_gantt_id' => 'required'
        ]);

        $code = $this->getCodeByType($request->type);

        $data = array(
            's_activity_id'=> $request->source,
            't_activity_id'=> $request->target,
            'type_link'=> $request->type,
            'code_link'=> $code,
            'version_gantt_id'=>$request->version_gantt_id
        );

        $data['created_by'] = $this->currentName;

        $result = Link::create($data);

        if($result){
            return response()->json(['status'=>'success', 'tid'=>$result->id, 'action'=>'inserted','message'=>'add data project  successfully!','code'=>200], 200);
        }else{
            return response()->json(['status'=>'failed', 'action'=>'error','message'=>'add data project  failed!','code'=>400], 400);
        }
    }

    public function update(Request $request, $id)
    {
        if(!$id || (int) $id < 0 || $id==""){
            return response()->json(['status'=>'failed', 'action'=>'error','message'=>'id is required!','code'=>400], 400);
        }

        $data = Link::find($id);

        $code = $this->getCodeByType($request->type);

        $dataUpdate = array(
            's_activity_id'=> $request->source,
            't_activity_id'=> $request->target,
            'type_link'=> $request->type,
            'code_link'=> $code,
            'lag'=> $request->lag,
            'updated_by'=> $this->currentName
        );

        if($data){
            $result = $data->update($dataUpdate);
        }else{
            return response()->json(['status'=>'failed', 'action'=>'error','message'=>'activity link not found!','code'=>400], 400);
            die();
        }
        
        
        if($result){
            return response()->json(['status'=>'success', 'action'=>'updated','message'=>'activity link successfully updated!','code'=>200], 200);
        }else{
            return response()->json(['status'=>'failed', 'action'=>'error','message'=>'activity link failed updated!','code'=>400], 400);
        }
    }

    public function delete($id)
    {
        if(!$id || (int) $id < 0 || $id==""){
            return response()->json(['status'=>'failed', 'action'=>'error','message'=>'id is required!','code'=>400], 400);
            die();
        }

        $data = Link::find($id);

        if($data){
            $delete = $data->delete();
        }else{
            return response()->json(['status'=>'failed', 'action'=>'error','message'=>'activity link not found!','code'=>400], 400);
            die();
        }
        

        if($delete){
            $deletAll = Link::where("parent_id", $id);
            $deletAll->delete();
            return response()->json(['status'=>'success', 'action'=>'deleted','message'=>'activity link successfully deleted!','code'=>200], 200);
        }else{
            return response()->json(['status'=>'failed', 'action'=>'error','message'=>'activity link failed deleted!','code'=>400], 400);
        }
    }

    private function getCodeByType($code){
        switch ($code) {
            case 1:
                return "SS";
                break;
            case 2:
                return "FF";
                break;
            case 3:
                return "SF";
                break;
            default:
                return "FS";
                break;
        }
    }
}