<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Document;
use App\Models\Project;

class DocumentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $project_id = $request->session()->get('project_id');
        $project = Project::find($project_id);
        $top_documents = $project->documents()->where('parent_id',0)->get();
        return view('documents.list', ['project' => $project, 'top_documents' => $top_documents]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $project_id = $request->session()->get('project_id');
        $project = Project::find($project_id);
        $top_documents = $project->documents()->where('parent_id',0)->get();
        return view('documents.create', ['project' => $project, 'top_documents' => $top_documents]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        $project_id = $request->session()->get('project_id');
        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'parent_id' => ['required', 'integer'],
        ]);

        // $user = 
        Document::create([
            'project_id' => $project_id,
            'name' => $request->name,
            'parent_id' => $request->parent_id,
        ]);

        return redirect(route('documents.index'));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Request $request,$id)
    {
        $project_id = $request->session()->get('project_id');
        $project = Project::find($project_id);
        $top_documents = $project->documents()->where('parent_id',0)->get();
        $document = Document::find($id);
        return view('documents.edit', ['project' => $project, 'top_documents' => $top_documents, 'document' => $document]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        $project_id = $request->session()->get('project_id');
        $request->validate([
            'name' => ['required', 'string', 'max:255'],
            'parent_id' => 'required|integer|different:id',
            // 'parent_id' => ['required', 'integer','lt:'.$id,'gt:'.$id],
        ]);

        $document = Document::find($id);
        $document->name = $request->name;
        $document->project_id = $project_id;
        $document->parent_id = $request->parent_id;
        $document->save();

        return redirect(route('documents.index'));
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        Document::findOrFail($id)->delete();
        return redirect()->route('documents.index');
    }
}
