<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Api\V1\Concerns\RespondsWithApi;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;

class BuddyController extends Controller
{
    use RespondsWithApi;

    public function register(Request $request)
    {
        $data = $request->validate([
            'pan' => ['nullable', 'string', 'max:40'],
            'bank_account' => ['nullable', 'string', 'max:80'],
            'bank_ifsc' => ['nullable', 'string', 'max:30'],
        ]);

        $buddy = DB::table('buddies')->where('user_id', $request->user()->id)->first();

        if ($buddy) {
            return $this->ok($buddy, 'Buddy already registered.');
        }

        $code = 'BDY' . strtoupper(Str::random(5));
        $id = DB::table('buddies')->insertGetId([
            'user_id' => $request->user()->id,
            'buddy_code' => $code,
            'pan_encrypted' => isset($data['pan']) ? encrypt($data['pan']) : null,
            'bank_account_encrypted' => isset($data['bank_account']) ? encrypt($data['bank_account']) : null,
            'bank_ifsc' => $data['bank_ifsc'] ?? null,
            'status' => 'active',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('referral_codes')->insert([
            'owner_type' => 'buddy',
            'owner_id' => $id,
            'code' => $code,
            'status' => 'active',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        return $this->ok(DB::table('buddies')->where('id', $id)->first(), 'Buddy registered.', [], 201);
    }

    public function profile(Request $request)
    {
        return $this->ok($this->buddy($request));
    }

    public function referralCode(Request $request)
    {
        $buddy = $this->buddy($request);
        if (! $buddy) {
            return $this->fail('BUDDY_NOT_REGISTERED', 'Register as buddy first.', [], 404);
        }

        return $this->ok(DB::table('referral_codes')->where('owner_type', 'buddy')->where('owner_id', $buddy->id)->first());
    }

    public function referrals(Request $request)
    {
        $buddy = $this->buddy($request);
        if (! $buddy) {
            return $this->fail('BUDDY_NOT_REGISTERED', 'Register as buddy first.', [], 404);
        }
        $code = DB::table('referral_codes')->where('owner_type', 'buddy')->where('owner_id', $buddy->id)->first();

        return $this->ok($code ? DB::table('referral_leads')->where('referral_code_id', $code->id)->latest('id')->paginate(20) : []);
    }

    public function rewards(Request $request)
    {
        $buddy = $this->buddy($request);
        if (! $buddy) {
            return $this->fail('BUDDY_NOT_REGISTERED', 'Register as buddy first.', [], 404);
        }

        return $this->ok(DB::table('referral_rewards')->where('buddy_id', $buddy->id)->latest('id')->paginate(20));
    }

    public function payouts(Request $request)
    {
        $buddy = $this->buddy($request);
        if (! $buddy) {
            return $this->fail('BUDDY_NOT_REGISTERED', 'Register as buddy first.', [], 404);
        }

        return $this->ok(DB::table('payout_items')->where('buddy_id', $buddy->id)->latest('id')->paginate(20));
    }

    private function buddy(Request $request): ?object
    {
        return DB::table('buddies')->where('user_id', $request->user()->id)->first();
    }
}
