<?php

namespace App\Services\WaterDepartment;

use App\Mail\SendMail;
use App\Models\Status;
use App\Services\CommonService;
use App\Services\CurlAPiService;
use App\Models\ServiceCredential;
use App\Models\WaterPrabhagWards;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Storage;
use App\Services\AapaleSarkarLoginCheckService;
use App\Models\WaterDepartment\WaterChangeInUse;

class ChangeInUseService
{
    protected $curlAPiService;
    protected $aapaleSarkarLoginCheckService;

    public function __construct(CurlAPiService $curlAPiService, AapaleSarkarLoginCheckService $aapaleSarkarLoginCheckService, CommonService $commonService)
    {
        $this->curlAPiService = $curlAPiService;
        $this->commonService = $commonService;
        $this->aapaleSarkarLoginCheckService = $aapaleSarkarLoginCheckService;
    }

    public function store($request)
    {
        DB::beginTransaction();

        try {
            $request['user_id'] = Auth::user()->id;
            $user = Auth::user();
            $request['service_id'] = "17";
            $request['ward_area'] = WaterPrabhagWards::where('ward_name', $request->zone)->value('id'); // returns a single value instead of a collection

            // Handle file uploads and store original file names
            if ($request->hasFile('application_documents')) {
                $request['application_document'] = $request->application_documents->store('water-department/change-in-use','public');
            }
            if ($request->hasFile('nodues_documents')) {
                $request['nodues_document'] = $request->nodues_documents->store('water-department/change-in-use','public');
            }
            $waterChangeInUse = WaterChangeInUse::create($request->except('application_no'));
            if($waterChangeInUse){

                $applicationNo = 'MBMC/' . $request['service_id'] . '/' . $waterChangeInUse->id . '/' . date('dmY');
                $waterChangeInUse->application_no = $applicationNo;
                $waterChangeInUse->save();

                $status = Status::create([
                    'user_id' =>$request['user_id'],
                    'service_id' =>17,
                    'department_id' =>5,
                    'table_id' => $waterChangeInUse->id
                ]);
                $this->commonService->store_approval_hierarchies($waterChangeInUse, $user, $status);
            }

            // code to send data to department
            // if ($request->hasFile('application_documents')) {
            //     $request['application_document'] = $this->curlAPiService->convertFileInBase64($request->file('application_documents'));
            // } else {
            //     $request['application_document'] = "";
            // }
            // if ($request->hasFile('nodues_documents')) {
            //     $request['nodues_document'] = $this->curlAPiService->convertFileInBase64($request->file('nodues_documents'));
            // } else {
            //     $request['nodues_document'] = "";
            // }
            // $request['user_id'] = (Auth::user()->user_id && Auth::user()->user_id != "") ? Auth::user()->user_id : Auth::user()->id;
            // $newData = $request->except(['_token', 'application_documents', 'nodues_documents']);
            // Log::info($newData);
            // $data = $this->curlAPiService->sendPostRequestInObject($newData, config('rtsapiurl.water') . 'WaterBillMicroService/WaterbillApi/ApleSarkarService/RequestForChangeInUseOfWaterConnection', '');

            // // Decode JSON string to PHP array
            // $data = json_decode($data, true);
            // if (isset($data['status']) && $data['status'] == "200") {
            //     // Access the application_no
            //     $applicationId = $data['applicationId'];
            //     WaterChangeInUse::where('id', $waterChangeInUse->id)->update([
            //         'application_no' => $applicationId
            //     ]);

            //     if (Auth::user()->is_aapale_sarkar_user) {
            //         $aapaleSarkarCredential = ServiceCredential::where('dept_service_id', $request->service_id)->first();
            //         $serviceDay = ($aapaleSarkarCredential->service_day) ? $aapaleSarkarCredential->service_day : 20;

            //         $send = $this->aapaleSarkarLoginCheckService->encryptAndSendRequestToAapaleSarkar(Auth::user()->trackid, $aapaleSarkarCredential->client_code, Auth::user()->user_id, $aapaleSarkarCredential->service_id, $applicationId, 'N', 'NA', 'N', 'NA', $serviceDay, date('Y-m-d', strtotime("+$serviceDay days")), config('rtsapiurl.amount'), config('rtsapiurl.requestFlag'), config('rtsapiurl.applicationStatus'), config('rtsapiurl.applicationPendingStatusTxt'), $aapaleSarkarCredential->ulb_id, $aapaleSarkarCredential->ulb_district, 'NA', 'NA', 'NA', $aapaleSarkarCredential->check_sum_key, $aapaleSarkarCredential->str_key, $aapaleSarkarCredential->str_iv, $aapaleSarkarCredential->soap_end_point_url, $aapaleSarkarCredential->soap_action_app_status_url);

            //         if (!$send) {
            //             $this->aapaleSarkarLoginCheckService->savePendingAapaleSarkarData($applicationId, $request->service_id, Auth::user()->user_id);
            //             DB::commit();
            //             return [true];
            //         }
            //     }
            //     // $subject = "Testing Subject";
            //     // $message = "Testing Message";
            //     // Mail::to($request->email_id)->send(new SendMail($subject, $message));
            // } else {
            //     DB::rollback();
            //     return [false, $data['error']];
            // }
            // end of code to send data to department

            DB::commit();
            return [true];
        } catch (\Exception $e) {
            DB::rollback();
            Log::error('Error in store method: ' . $e->getMessage());
            return [false, $e->getMessage()];
        }
    }


    public function edit($id)
    {
        return WaterChangeInUse::find($id);
    }

    public function update($request, $id)
    {
        DB::beginTransaction();
        try {
            // Find the existing record
            $waterChangeInUse = WaterChangeInUse::findOrFail($id);
            // Handle file uploads and update original file names
            if ($request->hasFile('application_documents')) {
                if ($waterChangeInUse && Storage::exists($waterChangeInUse->application_document)) {
                    Storage::delete($waterChangeInUse->application_document);
                }
                $request['application_document'] = $request->application_documents->store('water-department/change-in-use');
            }
            if ($request->hasFile('nodues_documents')) {
                if ($waterChangeInUse && Storage::exists($waterChangeInUse->nodues_document)) {
                    Storage::delete($waterChangeInUse->nodues_document);
                }
                $request['nodues_document'] = $request->nodues_documents->store('water-department/change-in-use');
            }
            // Update the rest of the fields
            $waterChangeInUse->update($request->all());


            // code to send data to department
            if ($request->hasFile('application_documents')) {
                $request['application_document'] = $this->curlAPiService->convertFileInBase64($request->file('application_documents'));
            } else {
                $request['application_document'] = "";
            }
            if ($request->hasFile('nodues_documents')) {
                $request['nodues_document'] = $this->curlAPiService->convertFileInBase64($request->file('nodues_documents'));
            } else {
                $request['nodues_document'] = "";
            }
            $request['application_no'] = $waterChangeInUse->application_no;
            $request['user_id'] = (Auth::user()->user_id && Auth::user()->user_id != "") ? Auth::user()->user_id : Auth::user()->id;
            $newData = $request->except(['_token', 'id', 'application_documents', 'nodues_documents']);
            $data = $this->curlAPiService->sendPostRequestInObject($newData, config('rtsapiurl.water') . 'WaterBillMicroService/WaterbillApi/ApleSarkarService/RequestForUpdateChangeInUseOfWaterConnection', '');

            // Decode JSON string to PHP array
            $data = json_decode($data, true);

            if (isset($data['status']) && $data['status'] == "200") {
                // Access the application_no
                DB::commit();
                return [true];
            } else {
                DB::rollback();
                return [false, $data['error']];
            }
            // end of code to send data to department
        } catch (\Exception $e) {
            DB::rollback();
            Log::info($e);
            return [false, $e->getMessage()];
        }
    }
}
