<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Follow;
use App\Models\User;
use App\Services\OneSignalService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class FollowController extends Controller
{
    public function follow(User $user, Request $request): JsonResponse
    {
        $authUser = $request->user();

        if ($authUser->id === $user->id) {
            return response()->json(['message' => 'Kendinizi takip edemezsiniz.'], 422);
        }

        $exists = Follow::where('follower_id', $authUser->id)
            ->where('following_id', $user->id)
            ->exists();

        if ($exists) {
            return response()->json(['message' => 'Zaten takip ediyorsunuz.'], 422);
        }

        Follow::create([
            'follower_id' => $authUser->id,
            'following_id' => $user->id,
        ]);

        try {
            $notificationsEnabled = \Schema::hasColumn('users', 'notifications_enabled')
                ? $user->notifications_enabled
                : true;

            if ($notificationsEnabled) {
                app(OneSignalService::class)->sendToUser(
                    $user->id,
                    'Yeni Takipçi',
                    "{$authUser->name} sizi takip etmeye başladı.",
                    [
                        'type' => 'new_follower',
                        'user_id' => $authUser->id,
                        'username' => $authUser->username,
                    ],
                );
            }
        } catch (\Exception $e) {
            \Log::warning('Takip bildirimi gönderilemedi: ' . $e->getMessage());
        }

        return response()->json([
            'message' => 'Takip edildi.',
            'followers_count' => $user->followers()->count(),
        ]);
    }

    public function unfollow(User $user, Request $request): JsonResponse
    {
        $deleted = Follow::where('follower_id', $request->user()->id)
            ->where('following_id', $user->id)
            ->delete();

        if (!$deleted) {
            return response()->json(['message' => 'Zaten takip etmiyorsunuz.'], 422);
        }

        return response()->json([
            'message' => 'Takip bırakıldı.',
            'followers_count' => $user->followers()->count(),
        ]);
    }

    public function followers(User $user): JsonResponse
    {
        $followers = $user->followers()
            ->select('users.id', 'users.name', 'users.username', 'users.avatar', 'users.role')
            ->paginate(20);

        return response()->json($followers);
    }

    public function following(User $user): JsonResponse
    {
        $following = $user->following()
            ->select('users.id', 'users.name', 'users.username', 'users.avatar', 'users.role')
            ->paginate(20);

        return response()->json($following);
    }

    public function isFollowing(User $user, Request $request): JsonResponse
    {
        $isFollowing = Follow::where('follower_id', $request->user()->id)
            ->where('following_id', $user->id)
            ->exists();

        return response()->json(['is_following' => $isFollowing]);
    }
}
