<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\AbrarPlanProject;
use Illuminate\Support\Facades\Auth;

class AbrarPlanProjectController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $projects = AbrarPlanProject::where('province_id', '=', Auth::user()->province_id)->get();

        return view('rmto-preview.pages.abrar-project-list', ['projects' => $projects]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $project = AbrarPlanProject::find($id);

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $project,
            'province' => $project->province
        ], 200);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 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 = AbrarPlanProject::find($id);

        $file_index = 0;
        $files_path = [];
        while (true) {
            if ($request['project-files-' . $file_index]) {
                $files_path[$file_index]['path'] = $request->file('project-files-' . $file_index)
                    ->store('abrar_plan_projects');
                $file_index++;
            } else {
                break;
            }
        }

        $project->projectHistories()->create([
            'user_id' => $request->user()->id,
            'previous_type' => $project->project_type,
            'previous_status' => $project->project_status,
            'previous_status_progress' => $project->project_status_progress,
            'previous_status_description' => $project->project_status_description,
            'previous_status_date' => $project->project_status_date,
            'previous_start_latlng' => $project->project_start_latlng,
            'previous_end_latlng' => $project->project_end_latlng, 
            'new_files' => json_encode($files_path)
        ]);

        $project->project_type = explode(',', $request->input('project-type')) ?? null;
        $project->project_status = $request->input('project-status') ?? null;
        $project->project_status_progress = $request->input('project-status-progress') ?? null;
        $project->project_status_description = $request->input('project-status-description') ?? null;
        $project->project_status_date = $request->input('project-status-date') ?? null;
        $project->project_start_latlng = explode(',', $request->input('project-start-latlng')) ?? null;
        $project->project_end_latlng = explode(',', $request->input('project-end-latlng')) ?? null;

        $project->save();

        if ($file_index > 0) {
            $project->files()->createMany($files_path);
        }

        return response()->json([
            'status' => 'succeed',
            'message' => '',
            'data' => $project
        ], 200);
    }
}
