<?php

namespace App\Http\Controllers\Franchise;

use App\Http\Controllers\Controller;
use App\Models\Franchise;
use Illuminate\Contracts\View\View;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Validation\Rule;

class ProfileController extends Controller
{
    public function index(): View
    {
        $franchise = Franchise::query()->where('created_by', auth()->id())->first();

        return view('franchise.profile', compact('franchise'));
    }

    public function update(Request $request): RedirectResponse
    {
        $franchise = Franchise::query()->where('created_by', auth()->id())->first();
        if (! $franchise) {
            return back()->withErrors(['franchise' => 'Franchise not found.']);
        }

        $validated = $request->validate([
            'name' => ['required', 'string', 'max:191'],
            'owner_name' => ['nullable', 'string', 'max:191'],
            'mobile' => ['nullable', 'string', 'max:20'],
            'email' => ['nullable', 'email', 'max:191'],
            'address' => ['nullable', 'string', 'max:500'],
            'city' => ['nullable', 'string', 'max:191'],
            'state' => ['nullable', 'string', 'max:191'],
            'pincode' => ['nullable', 'string', 'max:12'],
            'gst_number' => ['nullable', 'string', 'max:20'],
            'bank_account_holder' => ['nullable', 'string', 'max:191'],
            'bank_account_number' => ['nullable', 'string', 'max:50'],
            'bank_ifsc' => ['nullable', 'string', 'max:20'],
            'bank_name' => ['nullable', 'string', 'max:191'],
            'upi_id' => ['nullable', 'string', 'max:120'],
            'theme_preference' => ['nullable', Rule::in(['light', 'dark', 'auto'])],
            'shop_license' => ['nullable', 'file', 'mimes:jpg,jpeg,png,pdf', 'max:4096'],
            'gst_certificate' => ['nullable', 'file', 'mimes:jpg,jpeg,png,pdf', 'max:4096'],
        ]);

        if ($request->hasFile('shop_license')) {
            if ($franchise->shop_license_path) {
                Storage::disk('public')->delete($franchise->shop_license_path);
            }
            $validated['shop_license_path'] = $request->file('shop_license')->store('franchise/licenses', 'public');
        }

        if ($request->hasFile('gst_certificate')) {
            if ($franchise->gst_certificate_path) {
                Storage::disk('public')->delete($franchise->gst_certificate_path);
            }
            $validated['gst_certificate_path'] = $request->file('gst_certificate')->store('franchise/gst', 'public');
        }

        $themePreference = $validated['theme_preference'] ?? null;
        unset($validated['shop_license'], $validated['gst_certificate'], $validated['theme_preference']);

        $franchise->fill($validated)->save();
        if ($themePreference !== null) {
            $request->user()->forceFill([
                'theme_preference' => $themePreference,
                'dark_mode_enabled' => $themePreference === 'dark',
            ])->save();
        }

        return back()->with('status', 'Franchise profile updated.');
    }

    public function updatePreferences(Request $request): JsonResponse
    {
        $data = $request->validate([
            'theme_preference' => ['required', Rule::in(['light', 'dark', 'auto'])],
        ]);

        $request->user()->forceFill([
            'theme_preference' => $data['theme_preference'],
            'dark_mode_enabled' => $data['theme_preference'] === 'dark',
        ])->save();

        return response()->json(['status' => 'ok']);
    }
}
