<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Services\ProfilService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class ProfilController extends Controller
{
    protected $profilService;

    public function __construct(ProfilService $profilService)
    {
        $this->profilService = $profilService;
    }

    public function index()
    {
        return $this->profilService->getProfil();
    }

    public function store(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'id' => 'nullable',
                'nama' => 'required|string',
                'alamat' => 'required|string',
                'rt' => 'string',
                'rw' => 'string',
                'kelurahan' => 'string',
                'kecamatan' => 'string',
                'kabupaten' => 'string',
                'kode_pos' => 'string',
                'nomor_telp' => 'string',
                'email' => 'email',
                'website' => 'string',
                'logo' => 'string|nullable',
            ]);

            $role = $this->profilService->saveProfil($validator->validated());
            return response()->json($role, 201);
        } catch (\Exception $e) {
            return response()->json(['message' => $e->getMessage()], 400);
        }
    }
}
