<?php

namespace App\Modules\Pilkades\Http\Controllers\AdminDesa;

use App\Http\Controllers\Controller;
use App\Modules\Pilkades\Models\Contact;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Validation\Rule;
use Illuminate\View\View;

class ContactController extends Controller
{
    public function index(): View
    {
        $contacts = Contact::orderBy('urutan')->get();

        return view('pilkades::admin-desa.contact.index', compact('contacts'));
    }

    public function store(Request $request): RedirectResponse
    {
        $data = $request->validate([
            'jenis' => ['required', Rule::in(['whatsapp', 'email', 'telepon', 'alamat_kantor'])],
            'value' => ['required', 'string', 'max:255'],
            'urutan' => ['nullable', 'integer'],
        ]);

        $data['desa_id'] = Auth::user()->desa_id;

        Contact::create($data);

        return back()->with('success', 'Kontak berhasil ditambahkan.');
    }

    public function destroy(Contact $contact): RedirectResponse
    {
        $contact->delete();

        return back()->with('success', 'Kontak berhasil dihapus.');
    }
}
