<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\PropertyXTypes;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\DB;

class PropertyXTypesController extends Controller
{

    public function index()
    {
        try {
            $prop = PropertyXTypes::with(['property', 'type'])
            ->latest()
                ->paginate(10);
                

            return response()->json([
                'status' => 'success',
                'property' => $prop
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Failed to retrieve properties x types.',
                'error' => $e->getMessage()
            ], 500);
        }
    }
    
    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            'harga'         => 'required|numeric',
            'jumlah_kamar'  => 'nullable|integer|min:0',
            'lantai'        => 'required|integer|max:50',
        ]);

        if ($validator->fails()) {
            return response()->json(['success' => false, 'errors' => $validator->errors()], 422);
        }

        try {
            DB::table('property_x_types')
                ->where('id', $id)
                ->update([
                    'harga'         => $request->harga,
                    'jumlah_kamar'  => $request->jumlah_kamar ?? 0,
                    'lantai'        => $request->lantai,
                    'updated_at'    => now(),
                ]);

            return response()->json(['success' => true, 'message' => 'Pivot updated.']);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => 'Failed to update.', 'error' => $e->getMessage()], 500);
        }
    }


    public function destroy(string $id)
    {
        try {
            DB::table('property_x_types')
                ->where('id', $id)
                ->update(['deleted_at' => now()]);

            return response()->json(['success' => true, 'message' => 'Soft deleted successfully.']);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => 'Failed to soft delete.', 'error' => $e->getMessage()], 500);
        }
    }

    public function restore(string $id)
    {
        try {
            DB::table('property_x_types')
                ->where('id', $id)
                ->update(['deleted_at' => null]);

            return response()->json(['success' => true, 'message' => 'Restored successfully.']);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => 'Failed to restore.', 'error' => $e->getMessage()], 500);
        }
    }

    public function forceDelete($id)
    {
        try {
            // Ambil id_type dari property_x_types
            $propertyXType = DB::table('property_x_types')->where('id', $id)->first();

            if (!$propertyXType) {
                return response()->json(['success' => false, 'message' => 'Data not found.'], 404);
            }

            // Cek apakah id_type digunakan pada tabel rooms
            $usedInRooms = DB::table('rooms')->where('id_prop_typ', $propertyXType->id_type)->exists();

            if ($usedInRooms) {
                return response()->json([
                    'success' => false,
                    'message' => 'Tidak dapat menghapus, tipe ini terhubung dengan room lain. anda harus menghapus room tersebut'
                ], 422);
            }

            DB::table('property_x_types')
                ->where('id', $id)
                ->delete();

            return response()->json(['success' => true, 'message' => 'Force deleted successfully.']);
        } catch (\Exception $e) {
            return response()->json(['success' => false, 'message' => 'Failed to force delete.', 'error' => $e->getMessage()], 500);
        }
    }

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

            return response()->json([
                'success' => true,
                'message' => 'Data property x yang dihapus berhasil diambil.',
                'data' => $trashedTypes
            ], 200);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Gagal mengambil data property x yang dihapus.',
                'error' => $e->getMessage()
            ], 500);
        }
    }
}
