<?php

namespace App\Http\Controllers\Backend;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\SettingData;
use Illuminate\Support\Facades\Artisan;

class SettingController extends Controller
{
    public function termsAndConditionApi()
    {
        $termsAndCondition = SettingData::termsAndCondition()->first();
        return sendSuccessResponse([
            'type' => $termsAndCondition->type ?? null,
            'content' => $termsAndCondition->content ?? null
        ]);
    }

    public function privacyPolicyApi()
    {
        $privacyPolicy = SettingData::privacyPolicy()->first();
        return sendSuccessResponse([
            'type' => $privacyPolicy->type ?? null,
            'content' => $privacyPolicy->content ?? null
        ]);
    }

    public function returnAndRefundApi()
    {
        $returnAndRefund = SettingData::returnAndRefund()->first();
        return sendSuccessResponse([
            'type' => $returnAndRefund->type ?? null,
            'content' => $returnAndRefund->content ?? null
        ]);
    }



    public function termsAndCondition()
    {
        $tearmAndCondition = SettingData::termsAndCondition()->first();
        return view('Backend.Settings.tearmAndCondition', ['tearmAndCondition' => $tearmAndCondition]);
    }

    public function privacyPolicy()
    {
        $privacyPolicy = SettingData::privacyPolicy()->first();
        return view('Backend.Settings.privacyPolicy', ['privacyPolicy' => $privacyPolicy]);
    }

    public function returnAndRefund()
    {
        $returnAndRefund = SettingData::returnAndRefund()->first();
        return view('Backend.Settings.returnAndRefund', ['returnAndRefund' => $returnAndRefund]);
    }

    public function updateTermsAndCondition(Request $request)
    {
        try {
            $tearmAndCondition = SettingData::termsAndCondition()->first();
            if ($tearmAndCondition) {
                $tearmAndCondition->update([
                    'type' => 'terms_and_condition',
                    'content' => $request->content
                ]);
            } else {
                SettingData::create([
                    'type' => 'terms_and_condition',
                    'content' => $request->content
                ]);
            }
            toastr()->success('Tearm and Condition updated successfully');
            return redirect()->back();
        } catch (\Throwable $th) {
            toastr()->error('Tearm and Condition update failed');
            return redirect()->back();
        }
    }

    public function updatePrivacyPolicy(Request $request)
    {
        try {
            $privacyPolicy = SettingData::privacyPolicy()->first();
            if ($privacyPolicy) {
                $privacyPolicy->update([
                    'type' => 'privacy_policy',
                    'content' => $request->content
                ]);
            } else {
                SettingData::create([
                    'type' => 'privacy_policy',
                    'content' => $request->content
                ]);
            }
            toastr()->success('Privacy Policy updated successfully');
            return redirect()->back();
        } catch (\Throwable $th) {
            toastr()->error('Privacy Policy update failed');
            return redirect()->back();
        }
    }

    public function updateReturnAndRefund(Request $request)
    {
        try {
            $returnAndRefund = SettingData::returnAndRefund()->first();
            if ($returnAndRefund) {
                $returnAndRefund->update([
                    'type' => 'return_and_refund',
                    'content' => $request->content
                ]);
            } else {
                SettingData::create([
                    'type' => 'return_and_refund',
                    'content' => $request->content
                ]);
            }
            toastr()->success('Return and Refund updated successfully');
            return redirect()->back();
        } catch (\Throwable $th) {
            toastr()->error('Return and Refund update failed');
            return redirect()->back();
        }
    }

    public function optimizeClear()
    {
        try {
            Artisan::call('optimize:clear');
            Artisan::call('optimize');
            toastr()->success('Optimize clear successfully');
            return redirect()->back();
        } catch (\Throwable $th) {
            toastr()->error('Optimize clear failed');
            return redirect()->back();
        }
    }

    public function storageLink()
    {
        try {
            Artisan::call('storage:link');

            // can i call chmod -R 775 storage bootstrap/cache
            exec('chmod -R 775 storage bootstrap/cache');

            toastr()->success('Storage link created successfully');
            return redirect()->back();
        } catch (\Throwable $th) {
            toastr()->error('Storage link creation failed');
            return redirect()->back();
        }
    }
}
