<?php

namespace App\Http\Controllers\Web\Master;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\Wilayah\Provinsi;
use Illuminate\Support\Facades\Validator;
use Illuminate\Validation\Rule;

class ProvinsiController extends Controller
{
    public function index(Request $request)
    {
        $title = 'Master Provinsi';

        $perPage = $request->query('per_page', 10); 
        $search = $request->query('search', '');

        $provinsi = Provinsi::where('nama', 'like', "%$search%")
            ->orWhere('kode', 'like', "%$search%")
            ->orWhere('iso', 'like', "%$search%")
            ->paginate($perPage);
        
        return view('master.provinsi.index', compact('title', 'provinsi'));
    }

    public function getProvinsi()
    {
        $provinsi = Provinsi::select(['id', 'kode', 'nama', 'iso'])->get();
        return response()->json(['data' => $provinsi]);
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'kode' => ['required', Rule::unique(Provinsi::class, 'kode')],
            'nama' => ['required', Rule::unique(Provinsi::class, 'nama')],
            'path' => ['nullable'],
            'iso'  => ['required', Rule::unique(Provinsi::class, 'iso')],
        ]);

        if ($validator->fails()) {
            return response()->json([
                'status' => 'error',
                'message' => $validator->errors()->first()
            ], 422);
        }

        Provinsi::create([
            'kode'          => $request->kode,
            'nama'          => $request->nama,
            'path'          => $request->path,
            'iso'           => $request->iso,
            'created_by'    => auth()->id(),
            'updated_by'    => auth()->id(),
        ]);

        return response()->json([
            'status' => 'success', 
            'message' => 'Provinsi berhasil ditambahkan.'
        ]);
    }

    public function view($id)
    {
        $provinsi = Provinsi::find($id);

        if (!$provinsi) {
            return response()->json([
                'status'  => 'error',
                'message' => 'Data provinsi tidak ditemukan.'
            ], 404);
        }

        return response()->json([
            'status' => 'success',
            'data'   => $provinsi
        ]);
    }

    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'kode' => 'required|max:2',
            'nama' => 'required',
            'iso'  => 'required'
        ]);

        if ($validator->fails()) {
            return response()->json([
                'status'    => 'error',
                'message'   => $validator->errors()->first()
            ], 422);
        }

        $provinsi = Provinsi::find($id);
        
        if (!$provinsi) {
            return response()->json(['status' => 'error', 'message' => 'Data tidak ditemukan'], 404);
        }

        $provinsi->update([
            'kode'          => $request->kode,
            'nama'          => $request->nama,
            'path'          => $request->path,
            'iso'           => $request->iso,
            'updated_by'    => auth()->id(),
        ]);

        return response()->json(['status' => 'success', 'message' => 'Data berhasil diperbarui!']);
    }

    public function destroy($id)
    {
        $provinsi = Provinsi::find($id);
        if (!$provinsi) {
            return response()->json([
                'status'  => 'error',
                'message' => 'Data tidak ditemukan.'
            ], 404);
        }

        $provinsi->deleted_by = auth()->user()->id;
        $provinsi->save();
        $provinsi->delete();

        return response()->json([
            'status'  => 'success',
            'message' => 'Data provinsi berhasil dihapus.'
        ]);
    }
}