<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\LawmakersCorrespondence as Lawmaker;
use App\Http\Requests\Api\LawmakersRequest;
use App\Models\Unit;
use App\Models\Constituency;
use Hekmatinasser\Verta\Verta;
use Illuminate\Support\Facades\Auth;

// use Carbon\Carbon;

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

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


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


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

    public function update(LawmakersRequest $request, Lawmaker $lawmakers)
    {
        /// [START] proposal_edit_data_update
        if (!(Auth::user()->can('edit-lawmaker') || (Auth::user()->can('edit-lawmaker-province') && $request->province_id == Auth::user()->province_id))) {
            return response()->json([
                'message' => 'Forbidden'
            ], 403);
        }
        /// [END] proposal_edit_data_update
        
        $lawmakers->province_id = $request->province_id;
        $lawmakers->province_fa = $request->province_fa;
        $lawmakers->constituency_id = $request->constituency_id;
        $lawmakers->constituency_fa = $request->constituency_fa;
        $lawmakers->lawmaker_name = $request->lawmaker_name;
        $lawmakers->project_name = $request->project_name;
        $lawmakers->unit_id = $request->unit_id;
        $lawmakers->unit_fa = $request->unit_fa;
        $lawmakers->amount = $request->amount;
        $lawmakers->total_credit = $request->total_credit;
        $lawmakers->national_credit = $request->national_credit;
        $lawmakers->province_credit = $request->province_credit;
        $lawmakers->licence_credit = $request->licence_credit;
        $lawmakers->preaction_by_setad = $request->preaction_by_setad;
        $lawmakers->action_by_province = $request->action_by_province;
        $lawmakers->needed_actions = $request->needed_actions;
        $lawmakers->last_action = $request->last_action;
        $lawmakers->last_status_id = $request->last_status_id;
        $lawmakers->last_update_fa = Verta::now();
        $lawmakers->save();

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

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

    public function show(Lawmaker $lawmakers)
    {
        return response()->json([
            'message' => 'success',
            'data' => $lawmakers
        ]);
    }

    public function store(LawmakersRequest $request)
    {
        /// [START] lawmaker_add_data_store
        if (!(Auth::user()->can('add-lawmaker') || (Auth::user()->can('add-lawmaker-province') && $request->province_id == Auth::user()->province_id))) {
            return response()->json([
                'message' => 'Forbidden'
            ], 403);
        }
        /// [END] lawmaker_add_data_store
        
        $lawmakers = new Lawmaker();
        $lawmakers->province_id = $request->province_id;
        $lawmakers->province_fa = $request->province_fa;
        $lawmakers->constituency_id = $request->constituency_id;
        $lawmakers->constituency_fa = $request->constituency_fa;
        $lawmakers->lawmaker_name = $request->lawmaker_name;
        $lawmakers->project_name = $request->project_name;
        $lawmakers->unit_id = $request->unit_id;
        $lawmakers->unit_fa = $request->unit_fa;
        $lawmakers->amount = $request->amount;
        $lawmakers->total_credit = $request->total_credit;
        $lawmakers->national_credit = $request->national_credit;
        $lawmakers->province_credit = $request->province_credit;
        $lawmakers->licence_credit = $request->licence_credit;
        $lawmakers->preaction_by_setad = $request->preaction_by_setad;
        $lawmakers->action_by_province = $request->action_by_province;
        $lawmakers->needed_actions = $request->needed_actions;
        $lawmakers->last_action = $request->last_action;
        $lawmakers->last_status_id = $request->last_status_id;
        $lawmakers->last_update_fa = Verta::now();
        $lawmakers->save();


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

        Auth::user()->addActivity("مکاتبه با نماینده با کد یکتا {$lawmakers->unique_code} ایجاد شد.");

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

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

        Auth::user()->addActivity("   مکاتبه با نماینده با کد یکتا {$lawmaker->unique_code} حذف شد.");
        $lawmaker->delete();
        return response()->json([
            'message' => 'delete process is successfully'
        ]);
    }

    public function getUnits()
    {
        // dd();
        $data = Unit::all();
       
        return response()->json([
            'message' => 'success',
            'data' => $data
        ]);
    }

    public function getConstituencies($province_id)
    {
        $data = Constituency::where('province_id',$province_id)->get();
       
        return response()->json([
            'message' => 'success',
            'data' => $data
        ]);
    }
}