<?php

require __DIR__.'/vendor/autoload.php';

$app = require_once __DIR__.'/bootstrap/app.php';
$app->make('Illuminate\Contracts\Console\Kernel')->bootstrap();

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;

echo "=== Create Test Legal Agreement for User ===\n\n";

try {
    $userId = 30; // User ID
    $marketingOfficerId = 26; // Marketing Officer ID
    
    // Check if user exists
    $user = DB::table('users')->find($userId);
    if (!$user) {
        echo "❌ ERROR: User ID $userId not found\n";
        exit;
    }
    echo "✓ User exists: {$user->name} ({$user->email})\n";
    
    // Create test agreement file
    $testContent = "LEGAL AGREEMENT\n\nThis is a test legal agreement document.\n\nPlease sign and return.";
    $testPath = 'legal_agreements/test_agreement_' . time() . '.txt';
    
    Storage::disk('public')->put($testPath, $testContent);
    echo "✓ Test file created: $testPath\n";
    
    // Insert agreement into database
    $agreementId = DB::table('legal_agreements')->insertGetId([
        'user_id' => $userId,
        'marketing_officer_id' => $marketingOfficerId,
        'original_file_path' => $testPath,
        'status' => 'pending',
        'created_at' => now(),
        'updated_at' => now(),
    ]);
    
    echo "✓ Agreement created with ID: $agreementId\n\n";
    
    echo "=== Test Complete ===\n\n";
    echo "Now you can:\n";
    echo "1. Login as user: {$user->email}\n";
    echo "2. Go to dashboard at: http://localhost:5178/client/dashboard\n";
    echo "3. You should see the agreement in the Legal Agreements section\n";
    echo "4. Download the agreement\n";
    echo "5. Upload a signed version\n\n";
    
    echo "API endpoints used:\n";
    echo "- GET /api/v1/legal-agreements/user/$userId - Fetch agreements\n";
    echo "- POST /api/v1/legal-agreements/{id}/upload-signed - Upload signed version\n";
    
} catch (\Exception $e) {
    echo "\n❌ ERROR: " . $e->getMessage() . "\n";
    echo "\nStack trace:\n";
    echo $e->getTraceAsString() . "\n";
}
