<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use ZipArchive;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Str;
use App\Models\Category;
use App\Models\Brand;
use App\Models\Product;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB;
use App\Models\landlord\Tenant;


class AddonInstallController extends Controller
{
    public function saasInstall(Request $request)
    {
        $data = [
            'purchase_code' => $request->purchase_code,
            'product' => 'saas'
        ];
        $path = '';
        $module = 'saas';
        return $this->addonIstallUnzipMigrateRemoveTempFolder($data, $path, $module);
    }

    public function ecommerceInstall(Request $request)
    {
        $data = [
            'purchase_code' => $request->purchase_code,
            'product' => (config('database.connections.saleprosaas_landlord')) ? 'saas_ecom' : 'ecom'
        ];
        $path = '/Modules/';
        $module = 'ecommerce';
        return $this->addonIstallUnzipMigrateRemoveTempFolder($data, $path, $module);
    }

    public function woocommerceInstall(Request $request)
    {
        $data = [
            'purchase_code' => $request->purchase_code,
            'product' => (config('database.connections.saleprosaas_landlord')) ? 'saas_wcom' : 'wcom'
        ];
        $path = '/Modules/';
        $module = 'woocommerce';
        return $this->addonIstallUnzipMigrateRemoveTempFolder($data, $path, $module);
    }

    public function apiInstall(Request $request)
    {
        $data = [
            'purchase_code' => $request->purchase_code,
            'product' => (config('database.connections.saleprosaas_landlord')) ? 'saas_api' : 'api'
        ];
        $path = '/app/Http/Controllers/';
        $module = 'api';
        return $this->addonIstallUnzipMigrateRemoveTempFolder($data, $path, $module);
    }

    public function addonIstallUnzipMigrateRemoveTempFolder($data, $path, $module)
    {
        return redirect()->back()->with('not_permitted', 'Online addon installation is disabled for standalone local setup. Please place the addon files manually in the Modules/ directory.');
    }

    public function categorySlug()
    {
        $catgories = Category::select('id','name','slug')->get();
        foreach($catgories as $cat){
            $cat->slug = Str::slug($cat->name, '-');
            $cat->save();
        }
    }

    public function brandSlug()
    {
        $brands = Brand::select('id','title','slug')->get();
        foreach($brands as $brand){
            $brand->slug = Str::slug($brand->title, '-');
            $brand->save();
        }
    }

    public function productSlug()
    {
        $products = Product::select('id','name','slug')->get();
        foreach($products as $product){
            $product->slug = Str::slug($product->name, '-');
            $product->save();
        }
    }

}
