<?php

use App\Modules\Xero\Controllers\XeroApiController;
use App\Modules\Xero\Controllers\XeroAuthController;

// Xero Integration Routes
Route::group(['prefix' => 'api/v1/xero', 'middleware' => ['web', 'auth']], function() {
    
    // OAuth2 Authentication Routes
    Route::get('/auth', [XeroAuthController::class, 'redirectToProvider'])->name('xero.auth');
    Route::get('/callback', [XeroAuthController::class, 'handleCallback'])->name('xero.callback');
    Route::post('/test-connection', [XeroApiController::class, 'testConnection'])->name('xero.test');
    Route::get('/status', [XeroAuthController::class, 'getConnectionStatus'])->name('xero.status');
    Route::delete('/disconnect', [XeroAuthController::class, 'disconnect'])->name('xero.disconnect');
    
    // Bank Account Management
    Route::post('/sync-accounts', [XeroApiController::class, 'syncBankAccounts'])->name('xero.sync-accounts');
    
    // API Endpoints
    Route::get('/items', [XeroApiController::class, 'getItems'])->name('xero.items');
    Route::get('/accounts', [XeroApiController::class, 'getAccounts'])->name('xero.accounts');
    Route::get('/types', [XeroApiController::class, 'getTypes'])->name('xero.types');
    
    // Invoice Operations  
    Route::post('/invoices', [XeroApiController::class, 'createInvoice'])->name('xero.invoices.create');
    Route::get('/invoices/{id}', [XeroApiController::class, 'getInvoice'])->name('xero.invoices.get');
    Route::put('/invoices/{id}', [XeroApiController::class, 'updateInvoice'])->name('xero.invoices.update');
    
    // Purchase Order Operations
    Route::post('/purchase-orders', [XeroApiController::class, 'createPurchaseOrder'])->name('xero.purchase.create');
    Route::get('/purchase-orders/{id}', [XeroApiController::class, 'getPurchaseOrder'])->name('xero.purchase.get');
    
    // Contact Operations
    Route::get('/contacts', [XeroApiController::class, 'getContacts'])->name('xero.contacts');
    Route::post('/contacts', [XeroApiController::class, 'createContact'])->name('xero.contacts.create');
});

// Admin Integration Settings Routes
Route::group(['prefix' => 'api/v1/settings', 'middleware' => ['web', 'auth']], function() {
    Route::get('/integrations.xero', function() {
        return response()->json([
            'enabled' => \App\Models\Setting::get('integrations.xero.enabled', false),
            'client_id' => \App\Models\Setting::get('integrations.xero.client_id', ''),
            'client_secret' => \App\Models\Setting::get('integrations.xero.client_secret', ''),
            'redirect_uri' => \App\Models\Setting::get('integrations.xero.redirect_uri', ''),
            'connected' => \App\Models\Setting::get('integrations.xero.connected', false),
            'organization' => \App\Models\Setting::get('integrations.xero.organization', '')
        ]);
    });
    
    Route::post('/integrations.xero', function(\Illuminate\Http\Request $request) {
        try {
            \App\Models\Setting::set('integrations.xero.enabled', $request->input('enabled', false));
            \App\Models\Setting::set('integrations.xero.client_id', $request->input('client_id'));
            \App\Models\Setting::set('integrations.xero.client_secret', $request->input('client_secret'));
            \App\Models\Setting::set('integrations.xero.redirect_uri', $request->input('redirect_uri'));
            \App\Models\Setting::set('integrations.xero.connected', $request->input('connected', false));
            \App\Models\Setting::set('integrations.xero.organization', $request->input('organization', ''));
            
            return response()->json(['success' => true]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => $e->getMessage()
            ], 500);
        }
    });
}); 