<?php

namespace Modules\Internal\Http\Controllers\Api;

use App\Helpers\Helper;
use App\Models\Organization;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Internal\Transformers\OrganizationResource;

class OrganizationController extends Controller
{
    /**
     * Display a listing of the resource.
     * @return Renderable
     */
    public function index(Request $request)
    {
        $organizations = Organization::hospital()->with('currentAddress')
            ->when($request->search ?? null, function ($query, $search) {
                $query->where('name', 'LIKE', '%' . $search . '%');
            })
            ->paginate();
        return response()->json(Helper::paginateResources(OrganizationResource::collection($organizations)));
    }

    public function searchOrganization(Request $request)
    {
        $organizations = Organization::hospital()->get();
        return response()->json(OrganizationResource::collection($organizations));
    }

    /**
     * Show the form for creating a new resource.
     * @return Renderable
     */
    public function create()
    {
        return view('internal::create');
    }

    /**
     * Store a newly created resource in storage.
     * @param Request $request
     * @return Renderable
     */
    public function store(Request $request)
    {
        $organization = [
            'code' => $request->code,
            'name' => $request->name,
            'type' => 'hospital',
            'status' => $request->active == 1 ? 'active' : 'inactive',
            'description' => $request->description,
            'corporate_id_partner' => $request->corporate_id_partner
        ];

        $create_organization = Organization::create($organization);

        if ($request->phone != null) {
            $create_organization->metas()->create([
                'system' => 'default',
                'type' => 'phone',
                'value' => $request->phone,
            ]);
        }

        $address = $create_organization->addresses()->create([
            'use' => 'both',
            'type' => 'physical',
            'text' => $request->address,
            'province_id' => $request->province_id,
            'city_id' => $request->city_id,
            'district_id' => $request->district_id,
            'village_id' => $request->village_id,
            'postal_code' => $request->postal_code,
            'lat' => $request->lat,
            'lng' => $request->lng,
        ]);

        $create_organization->main_address_id = $address->id;
        $create_organization->save();

        return response()->json([
            'message' => 'Data berhasil disimpan',
            'data' => new OrganizationResource($create_organization)
        ]);
    }

    /**
     * Show the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function show($id)
    {
        return response()->json(OrganizationResource::make(Organization::find($id)));
    }

    /**
     * Show the form for editing the specified resource.
     * @param int $id
     * @return Renderable
     */
    public function edit($id)
    {
        return response()->json(OrganizationResource::make(Organization::find($id)));
    }

    /**
     * Update the specified resource in storage.
     * @param Request $request
     * @param int $id
     * @return Renderable
     */
    public function update(Request $request, $id)
    {
        $update_organization = Organization::find($id);

        $update_organization->update([
            'code' => $request->code,
            'name' => $request->name,
            'type' => 'hospital',
            'status' => $request->active == 1 ? 'active' : 'inactive',
            'description' => $request->description,
        ]);

        if ($request->phone != null) {
            $update_organization->metas()->updateOrCreate([
                'system' => 'default',
                'type' => 'phone',
            ], [
                'system' => 'default',
                'type' => 'phone',
                'value' => $request->phone,
            ]);
        }

        $newAddres = $update_organization->addresses()->updateOrCreate([
            'id' => $update_organization->main_address_id
        ], [
            'use' => 'both',
            'type' => 'hospital',
            'text' => $request->address,
            'province_id' => $request->province_id,
            'city_id' => $request->city_id,
            'district_id' => $request->district_id,
            'village_id' => $request->village_id,
            'postal_code' => $request->postal_code,
            'lat' => $request->lat,
            'lng' => $request->lng,
        ]);
        
        $update_organization->main_address_id = $newAddres->id;
        $update_organization->save();


        return response()->json([
            'message' => 'Data berhasil diubah',
            'data' => new OrganizationResource($update_organization)
        ]);
    }

    /**
     * Remove the specified resource from storage.
     * @param int $id
     * @return Renderable
     */
    public function destroy($id)
    {
        $delete_organization = Organization::find($id);
        $delete_organization->addresses()->delete();
        $delete_organization->delete();
        return response()->json([
            'message' => 'Data berhasil dihapus',
            'data' => new OrganizationResource($delete_organization)
        ]);
    }
}
