<?php

namespace App\Http\Controllers;

use App\Models\Property;
use Illuminate\Http\Request;

class PropertyController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        $properties = Property::all();
        return response()->json($properties);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $request->validate([
            'personal_id' => 'required',
            'assetAddress' => 'nullable',
            'assetArea' => 'nullable|integer',
            'assetType' => 'nullable',
            'assetUnit' => 'nullable',
            'assetValue' => [
                'nullable',
                function ($attribute, $value, $fail) use ($request) {
                    if ($request->input('assetType') === 'notApplicable' && $value !== null) {
                        $fail('The asset value must be null if asset type is not applicable.');
                    }
                }
            ],
            'propertyAddress' => 'nullable',
            'propertyArea' => 'nullable|integer',
            'propertyCategory' => 'nullable',
            'propertyType' => 'nullable',
            'propertyUnit' => 'nullable',
            'propertyValue' => [
                'nullable',
                function ($attribute, $value, $fail) use ($request) {
                    if ($request->input('propertyType') === 'Owned' && $value === null) {
                        $fail('The property value is required if property type is owned.');
                    }
                }
            ],
        ]);

        // Check if property record already exists for the personal_id
        $existingProperty = Property::where('personal_id', $request->input('personal_id'))->first();

        if ($existingProperty) {
            // Update existing property record
            $existingProperty->update($request->all());
            return response()->json($existingProperty, 200);
        } else {
            // Create new property record
            $property = Property::create($request->all());
            return response()->json($property, 201);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show($id)
    {
        $property = Property::findOrFail($id);
        return response()->json($property);
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id)
    {
        $property = Property::findOrFail($id);
    
        $request->validate([
            'personal_id' => 'required',
            'assetAddress' => 'nullable',
            'assetArea' => 'nullable|integer',
            'assetType' => 'nullable',
            'assetUnit' => 'nullable',
            'assetValue' => [
                'nullable',
                function ($attribute, $value, $fail) use ($request) {
                    if ($request->input('assetType') === 'notApplicable' && $value !== null) {
                        $fail('The asset value must be null if asset type is not applicable.');
                    }
                }
            ],
            'propertyAddress' => 'nullable',
            'propertyArea' => 'nullable|integer',
            'propertyCategory' => 'nullable',
            'propertyType' => 'nullable',
            'propertyUnit' => 'nullable',
            'propertyValue' => [
                'nullable',
                function ($attribute, $value, $fail) use ($request) {
                    if ($request->input('propertyType') === 'Owned' && $value === null) {
                        $fail('The property value is required if property type is owned.');
                    }
                }
            ],
        ]);
    
        $property->update($request->all());
    
        return response()->json($property, 200);
    }
    

    /**
     * Remove the specified resource from storage.
     */
    public function destroy($id)
    {
        $property = Property::findOrFail($id);
        $property->delete();

        return response()->json(null, 204);
    }
}
