<?php

namespace Tests\Feature\Feature;

use App\Models\BinaryLedger;
use App\Models\MemberTreeClosure;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class TreeManagementTest extends TestCase
{
    use RefreshDatabase;

    public function test_admin_can_view_tree_with_weak_leg_highlight(): void
    {
        $admin = User::query()->create([
            'member_code' => 'AKRADMIN100',
            'referral_code' => 'REFADMIN100',
            'name' => 'Admin User',
            'email' => 'admin-tree@example.com',
            'mobile' => '9100000100',
            'password' => 'password',
            'role' => 'admin',
            'status' => 'active',
        ]);

        $root = User::query()->create([
            'member_code' => 'AKRROOT100',
            'referral_code' => 'REFROOT100',
            'name' => 'Root Member',
            'email' => 'root@example.com',
            'mobile' => '9100000101',
            'password' => 'password',
            'role' => 'user',
            'status' => 'active',
        ]);

        $left = User::query()->create([
            'member_code' => 'AKRLEFT100',
            'referral_code' => 'REFLEFT100',
            'name' => 'Left Member',
            'email' => 'left@example.com',
            'mobile' => '9100000102',
            'password' => 'password',
            'role' => 'user',
            'status' => 'active',
            'placement_parent_id' => $root->id,
            'placement_leg' => 'left',
            'sponsor_id' => $root->id,
        ]);

        $right = User::query()->create([
            'member_code' => 'AKRRIGHT100',
            'referral_code' => 'REFRIGHT100',
            'name' => 'Right Member',
            'email' => 'right@example.com',
            'mobile' => '9100000103',
            'password' => 'password',
            'role' => 'user',
            'status' => 'active',
            'placement_parent_id' => $root->id,
            'placement_leg' => 'right',
            'sponsor_id' => $root->id,
        ]);

        MemberTreeClosure::query()->insert([
            [
                'ancestor_id' => $root->id,
                'descendant_id' => $root->id,
                'depth' => 0,
                'path_leg' => 'self',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'ancestor_id' => $root->id,
                'descendant_id' => $left->id,
                'depth' => 1,
                'path_leg' => 'left',
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'ancestor_id' => $root->id,
                'descendant_id' => $right->id,
                'depth' => 1,
                'path_leg' => 'right',
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);

        BinaryLedger::query()->create([
            'source_user_id' => null,
            'beneficiary_user_id' => $root->id,
            'leg' => 'left',
            'ledger_type' => 'joining',
            'direction' => 'credit',
            'units' => 100,
            'posted_at' => now(),
        ]);

        BinaryLedger::query()->create([
            'source_user_id' => null,
            'beneficiary_user_id' => $root->id,
            'leg' => 'right',
            'ledger_type' => 'joining',
            'direction' => 'credit',
            'units' => 40,
            'posted_at' => now(),
        ]);

        $response = $this->actingAs($admin)->get('/admin/tree?q='.$root->member_code.'&mode=placement&depth=3');

        $response->assertOk();
        $response->assertSee('Weak Leg');
        $response->assertSee('Right');
        $response->assertSee('AKRLEFT100');
        $response->assertSee('AKRRIGHT100');
    }

    public function test_admin_can_export_tree_csv(): void
    {
        $admin = User::query()->create([
            'member_code' => 'AKRADMIN200',
            'referral_code' => 'REFADMIN200',
            'name' => 'Admin User',
            'email' => 'admin-tree-2@example.com',
            'mobile' => '9100000200',
            'password' => 'password',
            'role' => 'admin',
            'status' => 'active',
        ]);

        $root = User::query()->create([
            'member_code' => 'AKRROOT200',
            'referral_code' => 'REFROOT200',
            'name' => 'Root Member',
            'email' => 'root-2@example.com',
            'mobile' => '9100000201',
            'password' => 'password',
            'role' => 'user',
            'status' => 'active',
        ]);

        MemberTreeClosure::query()->create([
            'ancestor_id' => $root->id,
            'descendant_id' => $root->id,
            'depth' => 0,
            'path_leg' => 'self',
        ]);

        $response = $this->actingAs($admin)->get('/admin/tree/export?q='.$root->member_code.'&mode=placement&format=csv');

        $response->assertOk();
        $response->assertHeader('content-disposition');
        $this->assertStringContainsString('.csv', (string) $response->headers->get('content-disposition'));
    }
}

