<?php
namespace App\Http\Controllers;

use App\Models\SystemInfo;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class SystemInfoController extends Controller
{
    public function index()
    {
        $items = SystemInfo::orderBy('id', 'desc')->paginate(20);
        return view('system_info.index', compact('items'));
    }

    public function create()
    {
        return view('system_info.create');
    }

    public function store(Request $request)
    {
        $data = $request->validate([
            'website_title' => ['required', 'string', 'max:191'],
            'company_name'  => ['required', 'string', 'max:191'],
            'website'       => ['nullable', 'string', 'max:191'],
            'email'         => ['nullable', 'email', 'max:191'],
            'phone'         => ['required', 'string', 'max:50'],
            'whatsapp'      => ['nullable', 'string', 'max:50'],

            'facebook'      => ['nullable', 'string', 'max:255'],
            'instagram'     => ['nullable', 'string', 'max:255'],
            'twitter'       => ['nullable', 'string', 'max:255'],
            'linkedin'      => ['nullable', 'string', 'max:255'],

            'logo'          => ['nullable', 'image', 'mimes:jpg,jpeg,png,webp', 'max:2048'],

            'address'       => ['nullable', 'string'],
            'about'         => ['nullable', 'string'],
            'footer_text'   => ['nullable', 'string'],
            'status'        => ['nullable', 'integer'],
        ]);

        if ($request->hasFile('logo')) {
            $data['logo'] = $request->file('logo')->store('system', 'public');
        }

        SystemInfo::create($data);

        return redirect()->route('system-info.index')->with('msg', 'System Info created successfully.');
    }

    public function show($id)
    {
        $item = SystemInfo::findOrFail($id);
        return view('system_info.show', compact('item'));
    }

    public function edit($id)
    {
        $item = SystemInfo::findOrFail($id);
        return view('system_info.edit', compact('item'));
    }

    public function update(Request $request, $id)
    {
        $item = SystemInfo::findOrFail($id);

        $data = $request->validate([
            'website_title' => ['required', 'string', 'max:191'],
            'company_name'  => ['required', 'string', 'max:191'],
            'website'       => ['nullable', 'string', 'max:191'],
            'email'         => ['nullable', 'email', 'max:191'],
            'phone'         => ['required', 'string', 'max:50'],
            'whatsapp'      => ['nullable', 'string', 'max:50'],

            'facebook'      => ['nullable', 'string', 'max:255'],
            'instagram'     => ['nullable', 'string', 'max:255'],
            'twitter'       => ['nullable', 'string', 'max:255'],
            'linkedin'      => ['nullable', 'string', 'max:255'],

            'logo'          => ['nullable', 'image', 'mimes:jpg,jpeg,png,webp', 'max:2048'],

            'address'       => ['nullable', 'string'],
            'about'         => ['nullable', 'string'],
            'footer_text'   => ['nullable', 'string'],
            'status'        => ['nullable', 'integer'],
        ]);

        if ($request->hasFile('logo')) {
            if ($item->logo && Storage::disk('public')->exists($item->logo)) {
                Storage::disk('public')->delete($item->logo);
            }
            $data['logo'] = $request->file('logo')->store('system', 'public');
        }

        $item->update($data);

        return redirect()->route('system-info.index')->with('msg', 'System Info updated successfully.');
    }

    public function destroy($id)
    {
        $item = SystemInfo::findOrFail($id);

        if ($item->logo && Storage::disk('public')->exists($item->logo)) {
            Storage::disk('public')->delete($item->logo);
        }

        $item->delete();

        return redirect()->route('system-info.index')->with('msg', 'System Info deleted.');
    }
}
