<?php

namespace App\Http\Controllers\Web;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Contractor;
use App\Models\Contract;  
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class AdminController extends Controller
{
    // ============================================================
    // USER MANAGEMENT
    // ============================================================
    public function users()
    {
        $users = User::orderBy('created_at', 'desc')->paginate(10);
        return view('admin.users', compact('users'));
    }

    public function createUser()
    {
        return view('admin.users-create');
    }

    public function storeUser(Request $request)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users',
            'password' => 'required|min:8',
            'role' => 'required|in:admin,ppk,contractor,agent,bank,auditor,super_admin',
        ]);

        $validated['password'] = Hash::make($validated['password']);
        $validated['is_active'] = true;

        User::create($validated);

        return redirect()->route('admin.users.index')->with('success', 'User berhasil ditambahkan!');
    }

    public function editUser(User $user)
    {
        return view('admin.users-edit', compact('user'));
    }

    public function updateUser(Request $request, User $user)
    {
        $validated = $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|unique:users,email,' . $user->id,
            'role' => 'required|in:admin,ppk,contractor,agent,bank,auditor,super_admin',
            'is_active' => 'boolean',
        ]);

        if ($request->filled('password')) {
            $validated['password'] = Hash::make($request->password);
        }

        $user->update($validated);

        return redirect()->route('admin.users.index')->with('success', 'User berhasil diupdate!');
    }

    public function deleteUser(User $user)
    {
        $user->delete();
        return redirect()->route('admin.users.index')->with('success', 'User berhasil dihapus!');
    }

    // ============================================================
    // CONTRACTOR MANAGEMENT
    // ============================================================
    public function contractors()
    {
        $contractors = Contractor::with('user')
            ->orderBy('created_at', 'desc')
            ->paginate(10);
        return view('admin.contractors', compact('contractors'));
    }

    public function createContractor()
    {
        return view('admin.contractors-create');
    }

    public function storeContractor(Request $request)
    {
        $validated = $request->validate([
            'user_id' => 'required|exists:users,id',
            'business_name' => 'required|string|max:255',
            'business_type' => 'required|in:pt,cv,koperasi,firma,perseorangan',
            'oap_status' => 'required|in:pending,verified,rejected',
            'address' => 'required|string',
            'province' => 'required|string',
            'regency' => 'required|string',
            'district' => 'required|string',
            'village' => 'required|string',
            'phone' => 'nullable|string',
            'email' => 'nullable|email',
        ]);

        Contractor::create($validated);

        return redirect()->route('admin.contractors.index')->with('success', 'Kontraktor berhasil ditambahkan!');
    }

    public function editContractor(Contractor $contractor)
    {
        return view('admin.contractors-edit', compact('contractor'));
    }

    public function updateContractor(Request $request, Contractor $contractor)
    {
        $validated = $request->validate([
            'business_name' => 'required|string|max:255',
            'business_type' => 'required|in:pt,cv,koperasi,firma,perseorangan',
            'oap_status' => 'required|in:pending,verified,rejected',
            'address' => 'required|string',
            'province' => 'required|string',
            'regency' => 'required|string',
            'district' => 'required|string',
            'village' => 'required|string',
            'phone' => 'nullable|string',
            'email' => 'nullable|email',
            'is_active' => 'boolean',
        ]);

        $contractor->update($validated);

        return redirect()->route('admin.contractors.index')->with('success', 'Kontraktor berhasil diupdate!');
    }

    public function deleteContractor(Contractor $contractor)
    {
        $contractor->delete();
        return redirect()->route('admin.contractors.index')->with('success', 'Kontraktor berhasil dihapus!');
    }

    public function showContractor(Contractor $contractor)
    {
        return view('admin.contractors-show', compact('contractor'));
    }

    // ============================================================
    // STATISTICS & OTHERS
    // ============================================================
    public function statistics()
    {
        return view('admin.statistics');
    }

    public function packages()
    {
        $packages = \App\Models\JobPackage::with(['ppk', 'selectedContractor'])
            ->orderBy('created_at', 'desc')
            ->paginate(10);
        return view('admin.packages', compact('packages'));
    }

    public function contracts()
    {
        $contracts = Contract::with(['jobPackage', 'contractor'])
            ->orderBy('created_at', 'desc')
            ->paginate(10);
        return view('admin.contracts', compact('contracts'));
    }
}