<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Property_Types;
use Illuminate\Support\Facades\Validator;

class PropertyTypesController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        try {
            $propertyTypes = Property_Types::all();
            return response()->json([
                'status' => 'success',
                'data' => $propertyTypes
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to retrieve property types: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * Display all property types including soft deleted ones.
     */
    public function indexAll()
    {
        try {
            $propertyTypes = Property_Types::withTrashed()->get();
            return response()->json([
                'status' => 'success',
                'data' => $propertyTypes
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to retrieve property types: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'type_name' => 'required|string|max:255|unique:property__types'
            ]);

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

            $propertyType = Property_Types::create($request->all());

            return response()->json([
                'status' => 'success',
                'message' => 'Property type created successfully',
                'data' => $propertyType
            ], 201);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to create property type: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show($id)
    {
        try {
            $propertyType = Property_Types::findOrFail($id);
            return response()->json([
                'status' => 'success',
                'data' => $propertyType
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to retrieve property type: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id)
    {
        try {
            $validator = Validator::make($request->all(), [
                'type_name' => 'required|string|max:255|unique:property__types,name_type,' . $id
            ]);

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

            $propertyType = Property_Types::findOrFail($id);
            $propertyType->update($request->all());

            return response()->json([
                'status' => 'success',
                'message' => 'Property type updated successfully',
                'data' => $propertyType
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to update property type: ' . $e->getMessage()
            ], 500);
        }
    }

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

            return response()->json([
                'status' => 'success',
                'message' => 'Property type deleted successfully'
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to delete property type: ' . $e->getMessage()
            ], 500);
        }
    }

    /**
     * Restore the specified soft deleted resource.
     */
    public function restore($id)
    {
        try {
            $propertyType = Property_Types::withTrashed()->findOrFail($id);
            $propertyType->restore();

            return response()->json([
                'status' => 'success',
                'message' => 'Property type restored successfully',
                'data' => $propertyType
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to restore property type: ' . $e->getMessage()
            ], 500);
        }
    }

    public function trashed()
    {
        try {
            $trashedFasility = Property_Types::onlyTrashed()->get();
            
            if ($trashedFasility->isEmpty()) {
                return response()->json([
                    'success' => true,
                    'message' => 'Tidak ada data type yang dihapus.',
                    'data' => []
                ], 200);
            }

            return response()->json([
                'success' => true,
                'message' => 'Data type yang dihapus berhasil diambil.',
                'data' => $trashedFasility
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Gagal mengambil data type yang dihapus.',
                'error' => $e->getMessage()
            ], 500);
        }
    }
    /**
     * Permanently delete the specified resource.
     */
    public function forceDestroy($id)
    {
        try {
            $propertyType = Property_Types::withTrashed()->findOrFail($id);
            $propertyType->forceDelete();

            return response()->json([
                'status' => 'success',
                'message' => 'Property type permanently deleted successfully'
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'status' => 'error',
                'message' => 'Failed to permanently delete property type: ' . $e->getMessage()
            ], 500);
        }
    }
}
