<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Proposal;
use App\Models\ProposalHistory;
use App\Http\Requests\Api\ProposalRequest;
use Illuminate\Support\Facades\Auth;
use Hekmatinasser\Verta\Verta;
use Illuminate\Support\Facades\Storage;

class ProposalController extends Controller
{
    public function index()
    {
        if (auth()->user()) {
            auth()->user()->addActivityComplete(1015);
        }
        /// [START] proposal_show_data_index
        if (Auth::user()->can('show-proposal')) {
            $data = Proposal::all();
        } elseif (Auth::user()->can('show-proposal-province')) {
            $data = Proposal::where('province_id', Auth::user()->province_id)->get();
        } else {
            return response()->json([
                'message' => 'Forbidden'
            ], 403);
        }
        /// [END] proposal_show_data_index

        $proposals = [];
        foreach ($data as $key => $value) {
            $proposals[$key] = $value;
            /// [START] proposal_edit_data_index
            if (Auth::user()->can('edit-proposal')) {
                $proposals[$key]['canEdit'] = 1;
            } elseif (Auth::user()->can('edit-proposal-province')) {
                if ($value->province_id == Auth::user()->province_id) {
                    $proposals[$key]['canEdit'] = 1;
                } else {
                    $proposals[$key]['canEdit'] = 0;
                }
            } else {
                $proposals[$key]['canEdit'] = 0;
            }
            /// [END] proposal_edit_data_index


            /// [START] proposal_delete_data_index
            if (Auth::user()->can('delete-proposal')) {
                $proposals[$key]['canDelete'] = 1;
            } elseif (Auth::user()->can('delete-proposal-province')) {
                if ($value->province_id == Auth::user()->province_id) {
                    $proposals[$key]['canDelete'] = 1;
                } else {
                    $proposals[$key]['canDelete'] = 0;
                }
            } else {
                $proposals[$key]['canDelete'] = 0;
            }
            /// [END] proposal_delete_data_index
        }

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

    public function show($id)
    {
        return response()->json([
            'message' => 'success',
            'data' => Proposal::find($id)
        ], 200);
    }

    public function store(ProposalRequest $request)
    {
        /// [START] proposal_add_data_store
        if (!(Auth::user()->can('add-proposal') || (Auth::user()->can('add-proposal-province') && $request->province_id == Auth::user()->province_id))) {
            return response()->json([
                'message' => 'Forbidden'
            ], 403);
        }
        /// [END] proposal_add_data_store


        $request->validate([
            'document_image' => 'required|file|max:2048|image',
            'document_technical' => 'file|max:5120',
            'document_video' => 'file|max:30720'
            ],
            ['حجم عکس حداکثر 3 مگابایت میباشد.',
            'حجم مستند حداکثر 5 مگابایت میباشد.',
            'حجم فیلم حداکثر 30 مگابایت میباشد.']
        );
        $proposal = new Proposal();

        $proposal->project_title = $request->project_title;
        $proposal->project_type_id = $request->project_type_id;
        $proposal->project_type_fa = $request->project_type_fa;
        $proposal->office_id = $request->office_id;
        $proposal->office_fa = $request->office_fa;
        $proposal->province_id = $request->province_id;
        $proposal->province_fa = \App\Models\Province::where('id', $request->province_id)->first()->name_fa;

        $proposal->axis_type_id = $request->axis_type_id;
        $proposal->axis_type_fa = $request->axis_type_fa;
        $proposal->axis_name = $request->axis_name;

        $proposal->followup_priority_project_id = $request->followup_priority_project_id;
        $proposal->followup_priority_project = $request->followup_priority_project;
        $proposal->priority_project = $request->priority_project;
        $proposal->priority_project_fa = $request->priority_project_fa;

        $proposal->status_id = 0;
        $proposal->status_fa = 'بررسی نشده';

        $proposal->operation_type_id1 = $request->operation_type_id1 ?? null;
        $proposal->operation_type_fa1 = $request->operation_type_fa1 ?? null;
        $proposal->operation_type_unit1 = $request->operation_type_unit1 ?? null;
        $proposal->operation_type_amount1 = $request->operation_type_amount1 ?? null;

        $proposal->operation_type_id2 = $request->operation_type_id2 ?? null;
        $proposal->operation_type_fa2 = $request->operation_type_fa2 ?? null;
        $proposal->operation_type_unit2 = $request->operation_type_unit2 ?? null;
        $proposal->operation_type_amount2 = $request->operation_type_amount2 ?? null;

        $proposal->operation_type_id3 = $request->operation_type_id3 ?? null;
        $proposal->operation_type_fa3 = $request->operation_type_fa3 ?? null;
        $proposal->operation_type_unit3 = $request->operation_type_unit3 ?? null;
        $proposal->operation_type_amount3 = $request->operation_type_amount3 ?? null;

        $proposal->operation_type_id4 = $request->operation_type_id4 ?? null;
        $proposal->operation_type_fa4 = $request->operation_type_fa4 ?? null;
        $proposal->operation_type_unit4 = $request->operation_type_unit4 ?? null;
        $proposal->operation_type_amount4 = $request->operation_type_amount4 ?? null;

        $proposal->start_lat = $request->start_lat;
        $proposal->end_lat = $request->end_lat;
        $proposal->start_lng = $request->start_lng;
        $proposal->end_lng = $request->end_lng;

        $proposal->follower_person = $request->follower_person;
        $proposal->follower_person_fa = $request->follower_person_fa;
        
        $proposal->user_id = Auth::user()->id;
        
        $proposal->user_description = $request->user_description;
        $proposal->national_credit = $request->national_credit;
        $proposal->province_credit = $request->province_credit;
        
        $proposal->created_at_fa = Verta::instance(Verta::now());
        $proposal->updated_at_fa = Verta::instance(Verta::now());
        
        $proposal->save();

        $proposal->document_image = $request->file('document_image')->storeAs('proposal_files/'.$proposal->id, 'document_image_'.$proposal->id.'.'.$request->file('document_image')->extension() ,'public');

        if ($request->has('document_technical')) {
            $proposal->document_technical = $request->file('document_technical')->storeAs('proposal_files/'.$proposal->id, 'document_technical_'.$proposal->id.'.'.$request->file('document_technical')->extension() ,'public');
        }

        if ($request->has('document_video')) {
            $proposal->document_video = $request->file('document_video')->storeAs('proposal_files/'.$proposal->id, 'document_video_'.$proposal->id.'.'.$request->file('document_video')->extension() ,'public');
        }

        $proposal->unique_code = $proposal->id + 10000;
        $proposal->save();

        Auth::user()->addActivity("پروژه پیشنهادی با کد یکتا {$proposal->unique_code} ایجاد شد.");

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

    }

    public function update(Request $request, Proposal $proposal)
    {
        /// [START] proposal_edit_data_update
        if (!(Auth::user()->can('edit-proposal') || (Auth::user()->can('edit-proposal-province') && $request->province_id == Auth::user()->province_id))) {
            return response()->json([
                'message' => 'Forbidden'
            ], 403);
        }
        /// [END] proposal_edit_data_update

        $request->validate([
            'document_image' => 'sometimes|file|max:2048|image',
            'document_technical' => 'sometimes|file|max:5120',
            'document_video' => 'sometimes|file|max:30720'
            ],
            ['حجم عکس حداکثر 3 مگابایت میباشد.',
            'حجم مستند حداکثر 5 مگابایت میباشد.',
            'حجم فیلم حداکثر 30 مگابایت میباشد.']
        );

        ProposalHistory::create([
            'project_title' => $proposal->project_title ?? null,
            'project_type_id' => $proposal->project_type_id ?? null,
            'project_type_fa' => $proposal->project_type_fa ?? null,
            'office_id' => $proposal->office_id ?? null,
            'office_fa' => $proposal->office_fa ?? null,
            'province_id' => $proposal->province_id ?? null,
            'province_fa' => $proposal->province_fa ?? null,
            'axis_type_id' => $proposal->axis_type_id ?? null,
            'axis_type_fa' => $proposal->axis_type_fa ?? null,
            'axis_name' => $proposal->axis_name ?? null,
            'followup_priority_project_id' => $proposal->followup_priority_project_id ?? null,
            'followup_priority_project' => $proposal->followup_priority_project ?? null,
            'priority_project' => $proposal->priority_project ?? null,
            'priority_project_fa' => $proposal->priority_project_fa ?? null,
            'status_id' => $proposal->status_id ?? null,
            'status_fa' => $proposal->status_fa ?? null,
            'operation_type_id1' => $proposal->operation_type_id1 ?? null,
            'operation_type_fa1' => $proposal->operation_type_fa1 ?? null,
            'operation_type_unit1' => $proposal->operation_type_unit1 ?? null,
            'operation_type_amount1' => $proposal->operation_type_amount1 ?? null,
            'operation_type_id2' => $proposal->operation_type_id2 ?? null,
            'operation_type_fa1' => $proposal->operation_type_fa1 ?? null,
            'operation_type_unit1' => $proposal->operation_type_unit1 ?? null,
            'operation_type_amount2' => $proposal->operation_type_amount2 ?? null,
            'operation_type_id3' => $proposal->operation_type_id3 ?? null,
            'operation_type_fa1' => $proposal->operation_type_fa1 ?? null,
            'operation_type_unit1' => $proposal->operation_type_unit1 ?? null,
            'operation_type_amount3' => $proposal->operation_type_amount3 ?? null,
            'operation_type_id4' => $proposal->operation_type_id4 ?? null,
            'operation_type_fa1' => $proposal->operation_type_fa1 ?? null,
            'operation_type_unit1' => $proposal->operation_type_unit1 ?? null,
            'operation_type_amount4' => $proposal->operation_type_amount4 ?? null,
            'start_lat' => $proposal->start_lat ?? null,
            'end_lat' => $proposal->end_lat ?? null,
            'start_lng' => $proposal->start_lng ?? null,
            'end_lng' => $proposal->end_lng ?? null,
            'user_id' => $proposal->user_id ?? null,
            'user_description' => $proposal->user_description ?? null,
            'national_credit' => $proposal->national_credit ?? null,
            'province_credit' => $proposal->province_credit ?? null,
            'created_at_fa' => $proposal->created_at_fa ?? null,
            'updated_at_fa' => $proposal->updated_at_fa ?? null,
            'unique_code' => $proposal->unique_code ?? null,
            'proposal_id' => $proposal->id,
        ]);

        if ($request->has('document_image')) {
            Storage::delete('public/'.$proposal->document_image);
            $proposal->document_image = $request->file('document_image')->storeAs('proposal_files/'.$proposal->id,'document_image_'.$proposal->id.'.'.$request->file('document_image'), 'public');
        }

        if ($request->has('document_technical')) {
            Storage::delete('public/'.$proposal->document_technical);
            $proposal->document_technical = $request->file('document_technical')->storeAs('proposal_files/'.$proposal->id,'document_technical_'.$proposal->id.'.'.$request->file('document_technical')->extension(), 'public');
        }

        if ($request->has('document_video')) {
            Storage::delete('public/'.$proposal->document_video);
            $proposal->document_video = $request->file('document_video')->storeAs('proposal_files/'.$proposal->id,'document_video_'.$proposal->id.'.'.$request->file('document_video'), 'public');
        }

        $proposal->project_title = $request->project_title;
        $proposal->project_type_id = $request->project_type_id;
        $proposal->project_type_fa = $request->project_type_fa;
        $proposal->office_id = $request->office_id;
        $proposal->office_fa = $request->office_fa;
        $proposal->province_id = $request->province_id;
        $proposal->province_fa = \App\Models\Province::where('id', $request->province_id)->first()->name_fa;

        $proposal->axis_type_id = $request->axis_type_id;
        $proposal->axis_type_fa = $request->axis_type_fa;
        $proposal->axis_name = $request->axis_name;

        $proposal->followup_priority_project_id = $request->followup_priority_project_id;
        $proposal->followup_priority_project = $request->followup_priority_project;
        $proposal->priority_project = $request->priority_project;
        $proposal->priority_project_fa = $request->priority_project_fa;

        $proposal->status_id = $request->status_id;
        $proposal->status_fa = $request->status_fa;

        $proposal->operation_type_id1 = $request->operation_type_id1 ?? null;
        $proposal->operation_type_fa1 = $request->operation_type_fa1 ?? null;
        $proposal->operation_type_unit1 = $request->operation_type_unit1 ?? null;
        $proposal->operation_type_amount1 = $request->operation_type_amount1 ?? null;

        $proposal->operation_type_id2 = $request->operation_type_id2 ?? null;
        $proposal->operation_type_fa2 = $request->operation_type_fa2 ?? null;
        $proposal->operation_type_unit2 = $request->operation_type_unit2 ?? null;
        $proposal->operation_type_amount2 = $request->operation_type_amount2 ?? null;

        $proposal->operation_type_id3 = $request->operation_type_id3 ?? null;
        $proposal->operation_type_fa3 = $request->operation_type_fa3 ?? null;
        $proposal->operation_type_unit3 = $request->operation_type_unit3 ?? null;
        $proposal->operation_type_amount3 = $request->operation_type_amount3 ?? null;

        $proposal->operation_type_id4 = $request->operation_type_id4 ?? null;
        $proposal->operation_type_fa4 = $request->operation_type_fa4 ?? null;
        $proposal->operation_type_unit4 = $request->operation_type_unit4 ?? null;
        $proposal->operation_type_amount4 = $request->operation_type_amount4 ?? null;

        $proposal->start_lat = $request->start_lat;
        $proposal->end_lat = $request->end_lat;
        $proposal->start_lng = $request->start_lng;
        $proposal->end_lng = $request->end_lng;

        $proposal->user_description = $request->user_description;
        $proposal->national_credit = $request->national_credit;
        $proposal->province_credit = $request->province_credit;

        $proposal->updated_at_fa = Verta::instance(Verta::now());

        $proposal->save();

        Auth::user()->addActivity("  پروژه پیشنهادی با کد یکتا {$proposal->unique_code} ویرایش شد.");

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

    }

    public function getHistory(Proposal $proposal)
    {
        return response()->json([
            'message' => 'success',
            'data' => ProposalHistory::where('proposal_id', $proposal->id)->get()
        ], 200);
    }

    public function delete(Proposal $proposal)
    {
        /// [START] proposal_delete_data_delete
        if (!(Auth::user()->can('delete-proposal') || (Auth::user()->can('delete-proposal-province') && $proposal->province_id == Auth::user()->province_id))) {
            return response()->json([
                'message' => 'Forbidden'
            ], 403);
        }
        /// [END] proposal_delete_data_delete

        Auth::user()->addActivity("  پروژه پیشنهادی با کد یکتا {$proposal->unique_code} حذف شد.");
        Storage::deleteDirectory('public/'.'proposal_files/'.$proposal->id);
        $proposal->delete();
        return response()->json([
            'message' => 'delete process is successfully'
        ]);
    }
}