<?php

namespace Modules\Admin\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;
use Illuminate\Database\QueryException;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Maatwebsite\Excel\Facades\Excel;
use Carbon\Carbon;

use App\Models\User;
use App\Models\AdminDetails;
use App\Models\Companies;
use App\Models\Supliers;
use App\Models\Categories;
use App\Models\Brands;
use App\Models\Products;
use App\Models\ProductGallery;
use App\Models\Banners;
use App\Models\Services;

use File;
use Mail;
use Image;
use PDF;
class ServiceController extends Controller
{

    public function add_service(Request $request)
    {
        if($request->isMethod('get')){
            $parent_categories = Categories::where('status','active')->where('parent_category', 'none')->get();
            // $serviceID = $this->generateUniqueServiceID();
            return view('admin::service.add_service',[
                'parent_categories' => $parent_categories,
            ]);
        }

        if($request->isMethod('post')){
            $request->validate([
                'name'   => 'required',
                'code'   => 'required',
                'price'   => 'required',
            ]);
            $serviceID = $this->generateUniqueServiceID();
            if($request->featured_image == null){
                $featured_image =  "-";
                $featured_image_url =  "-";
            } else {
                $featured_image = time().'-featured-.'.$request->featured_image->extension();
                $request->featured_image->move(public_path('db_files/service_images'), $featured_image);
                $featured_image_url = asset('db_files/service_images/').'/'.$featured_image;
            }

            $service = new Services();
            $service->service_id = $serviceID;
            $service->name = $request->name;
            $service->code = $request->code;
            $service->price = $request->price;
            $service->parent_category = $request->parent_category;
            $service->sub_category = $request->sub_category;
            $service->price_variant = $request->price_variant;
            $service->featured_image = $featured_image;
            $service->featured_image_url = $featured_image_url;
            $service->status = "active";
            $service->save();

            return back()->with('success', 'Service Successfully Added');
        }
    }


public function services(Request $request)
    { if($request->isMethod('get')){
        $services = Services::get();
        return view('admin::service.services',['services' => $services]);
    }

    }

    public function deactivate_service($id){
        $service = Services::find($id);
        $service->status = "inactive";
        $service->update();
        return back()->with('success', 'Service Deactivated');

    }
    public function activate_service($id){
        $service = Services::find($id);
        $service->status = "active";
        $service->update();
        return back()->with('success', 'Service Activated');

    }

    public function edit_service($id,Request $request)
    { if($request->isMethod('get')){
        $parent_categories = Categories::where('status','active')->where('parent_category', 'none')->get();
        $service_details = Services::where('id',$id)->first();
        return view('admin::service.edit_service',['parent_categories' => $parent_categories,'service_details' => $service_details]);
    }
    if($request->isMethod('post')){
        $request->validate([
            'name'   => 'required',
            'code'   => 'required',
            'price'   => 'required',
        ]);

        if($request->featured_image == null){
            $featured_image =  Services::where('id', $id)->value('featured_image');
            $featured_image_url =  asset('db_files/service_images/').'/'.$featured_image;
        }
        else{
            $featured_image = time().'-featured-.'.$request->featured_image->extension();
            $request->featured_image->move(public_path('db_files/service_images'), $featured_image);
            $featured_image_url = asset('db_files/service_images/').'/'.$featured_image;
        }

        $service = Services::where('id', $id)->first();
        $service->name = $request->name;
        $service->code = $request->code;
        $service->price = $request->price;
        $service->parent_category = $request->parent_category;
        $service->sub_category = $request->sub_category;
        $service->price_variant = $request->price_variant;
        $service->featured_image = $featured_image;
        $service->featured_image_url = $featured_image_url;
        $service->update();


        return back()->with('success', 'Service Successfully Updated');

    }

    }

    public function delete_service($id,Request $request)
    {
        Services::find($id)->delete();
        return back()->with('success', 'Service Deleted');
    }
    private function generateUniqueServiceID()
    {
        $year = Carbon::now()->format('y'); 
        $month = Carbon::now()->format('m');
    
        $lastId = Services::max('id') ?? 0;
        $nextId = str_pad($lastId + 1, 5, '0', STR_PAD_LEFT);
    
        $serviceID = "S{$year}{$month}{$nextId}";
    
        return $serviceID;
    }
}