<?php

namespace App\Http\Controllers;

use App\Models\Business;
use Illuminate\Support\Facades\Storage;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class BusinessController extends Controller
{
    public function index()
    {
        $businesses = Business::where('owner_id', Auth::id())->get();

        // dd($businesses);
        return view('settings.settings-general', compact('businesses'));
    }

    // public function save(Request $request)
    // {
    //     $business = Business::where('owner_id', Auth::id())->first();

    //     if ($business) {
    //         // Update existing business
    //         $business->update($request->only(['name', 'address', 'city', 'state', 'zip', 'phone', 'website', 'business_information']));
    //     } else {
    //         // Create a new business
    //         Business::create([
    //             'owner_id' => Auth::id(),
    //             'name' => $request->name,
    //             'address' => $request->address,
    //             'city' => $request->city,
    //             'state' => $request->state,
    //             'zip' => $request->zip,
    //             'phone' => $request->phone,
    //             'website' => $request->website,
    //             'business_information' => $request->business_information,
    //         ]);
    //     }

    //     return redirect()->back()->with('success', 'Business saved successfully.');
    // }

    public function store(Request $request)
    {

        // dd($request);
        $validatedData = $request->validate([
            'name' => 'required|string|max:255',
            'address' => 'nullable|string|max:255',
            'city' => 'nullable|string|max:255',
            'state' => 'nullable|string|max:255',
            'zip' => 'nullable|integer',
            'phone' => 'nullable|string|max:15',
            'website' => 'nullable|string|max:255',
            'business_information' => 'nullable|string',
        ]);

        // Add owner_id to validated data
        $validatedData['owner_id'] = auth()->id();

        if ($request->hasFile('image')) {
            $validatedData['image'] = $request->file('image')->store('business_images', 'public');
        }

        // dd($validatedData);
        Business::create($validatedData);

        return redirect()->back()->with('success', 'Business created successfully.');
    }


    // Update method
    public function update(Request $request, $id)
    {
        $business = Business::findOrFail($id);

        $validatedData = $request->validate([
            'name' => 'required|string|max:255',
            'image' => 'nullable|image|mimes:jpeg,png,jpg,gif,svg,webp|max:2048',
            'address' => 'nullable|string|max:255',
            'city' => 'nullable|string|max:255',
            'state' => 'nullable|string|max:255',
            'zip' => 'nullable|integer',
            'phone' => 'nullable|string|max:15',
            'website' => 'nullable|string|max:255',
            'business_information' => 'nullable|string',
        ]);

        // dd($request);
        $validatedData['owner_id'] = auth()->id();

        if ($request->hasFile('image')) {
            $uploadedFile = $request->file('image');
            if ($business->image) {
                Storage::disk('public')->delete($business->image);
            }

            $path = $uploadedFile->store('business_images', 'public');
            $validatedData['image'] = $path;
        } else {
            $validatedData['image'] = $business->image;
        }

        $business->update($validatedData);

        return redirect()->back()->with('success', 'Business updated successfully.');
    }
}
