<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\User;
use App\Models\Subscription;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Str;

class AuthController extends Controller
{
    public function login(Request $request)
    {
        $request->validate([
            'email' => 'required|email',
            'password' => 'required',
        ]);

        $user = User::where('email', $request->email)->first();

        if (!$user || !Hash::check($request->password, $user->password)) {
            return response()->json([
                'message' => 'Email atau password yang Anda masukkan salah.'
            ], 401);
        }

        $token = $user->createToken('auth_token')->plainTextToken;

        $user->load(['umkm', 'subscriptions']);

        $activeSubscription = $user->subscriptions()->latest()->first();

        return response()->json([
            'token' => $token,
            'user' => [
                'id' => $user->id,
                'name' => $user->name,
                'email' => $user->email,
                'role' => $user->role,
                'umkm' => $user->umkm,
                'is_profile_completed' => $user->umkm !== null,
                'subscription' => $activeSubscription ? [
                    'status' => $activeSubscription->status,
                    'package_name' => $activeSubscription->package_name,
                    'expires_at' => $activeSubscription->expires_at,
                ] : null,
            ],
        ]);
    }

    public function register(Request $request)
    {
        $request->validate([
            'name' => 'required|string|max:255',
            'email' => 'required|email|max:255|unique:users,email',
            'password' => 'required|string|min:8|confirmed',
        ]);

        $user = User::create([
            'name' => $request->name,
            'email' => $request->email,
            'password' => Hash::make($request->password),
            'role' => 'user',
        ]);

        Subscription::create([
            'user_id' => $user->id,
            'package_name' => 'Trial',
            'price' => 0,
            'starts_at' => now(),
            'expires_at' => now()->addDays(14),
            'status' => 'trial',
        ]);

        $token = $user->createToken('auth_token')->plainTextToken;

        $user->load(['umkm', 'subscriptions']);

        $activeSubscription = $user->subscriptions()->latest()->first();

        return response()->json([
            'token' => $token,
            'user' => [
                'id' => $user->id,
                'name' => $user->name,
                'email' => $user->email,
                'role' => $user->role,
                'umkm' => $user->umkm,
                'is_profile_completed' => $user->umkm !== null,
                'subscription' => $activeSubscription ? [
                    'status' => $activeSubscription->status,
                    'package_name' => $activeSubscription->package_name,
                    'expires_at' => $activeSubscription->expires_at,
                ] : null,
            ],
        ], 201);
    }
}
