<?php

namespace App\Http\Controllers\User;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use PHPOpenSourceSaver\JWTAuth\Facades\JWTAuth;

class UserProfileController extends Controller
{
    public function profile()
    {
        // Get logged-in user using JWT
       try {
            $user = JWTAuth::parseToken()->authenticate();
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'Token invalid or expired',
            ], 401);
        }


        if (! $user) {
            return response()->json([
                'success' => false,
                'message' => 'Unauthorized'
            ], 401);
        }

        // Format response
        return response()->json([
            'success' => true,
            'data' => [
                'customer_id'   => $user->customer_id,
                'customer_name' => $user->customer_name,
                'company_name'  => $user->company_name,
                'designation'   => $user->designation,
                'email'         => $user->email,
                'mobile'        => $user->mobile_no,
                'address'       => $user->address,
                'pincode'       => $user->pincode,
                'status'        => $user->status,
                'customer_type' => $user->customer_type,
                'profile_image' => $user->profile_image ? url($user->profile_image) : null,
                'date'          => $user->date,
                'created_at'    => $user->created_at,
            ]
        ]);
    }

    public function updateProfile(Request $request)
{
    $user = JWTAuth::parseToken()->authenticate();

    if (! $user) {
        return response()->json([
            'success' => false,
            'message' => 'Unauthorized'
        ], 401);
    }

    // Validate ONLY profile image
    $request->validate([
        'profile_image' => 'required|image|mimes:jpg,jpeg,png|max:2048'
    ]);

    // If new image uploaded
    if ($request->hasFile('profile_image')) {

        // DELETE OLD IMAGE
        if ($user->profile_image && file_exists(public_path($user->profile_image))) {
            @unlink(public_path($user->profile_image));
        }

        // NEW FILE
        $file = $request->file('profile_image');
        $filename = 'profile_' . time() . '.' . $file->getClientOriginalExtension();

        // Correct folder name (from your message)
        $folder = 'profile_upload/';
        $fullPath = $folder . $filename;

        // Move file
        $file->move(public_path($folder), $filename);

        // Save in DB (same pattern you said)
        $user->profile_image = $fullPath;
    }

    $user->save();

    return response()->json([
        'success' => true,
        'message' => 'Profile image updated successfully',
        'data' => [
            'customer_id'   => $user->customer_id,
            'customer_name' => $user->customer_name,
            'company_name'  => $user->company_name,
            'designation'   => $user->designation,
            'email'         => $user->email,
            'mobile'        => $user->mobile_no,
            'address'       => $user->address,
            'pincode'       => $user->pincode,
            'status'        => $user->status,
            'customer_type' => $user->customer_type,
            'profile_image' => $user->profile_image ? url($user->profile_image) : null,
            'date'          => $user->date,
        ]
    ]);
}

}
