<?php

use App\Http\Controllers\CallsController;
use App\Http\Controllers\ContactsController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\EmailsController;
use App\Http\Controllers\GroupsController;
use App\Http\Controllers\ImportsController;
use App\Http\Controllers\InstallerController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\SmsController;
use App\Http\Controllers\TagsController;
use App\Http\Controllers\TrackingController;
use App\Http\Controllers\Webhooks\TwilioWebhookController;
use App\Http\Controllers\WorkspaceController;
use Illuminate\Support\Facades\Route;

// Installer (only accessible until storage/app/installed.lock exists)
Route::prefix('install')->group(function () {
    Route::get('/', [InstallerController::class, 'welcome'])->name('install.welcome');
    Route::get('/requirements', [InstallerController::class, 'requirements'])->name('install.requirements');
    Route::get('/database', [InstallerController::class, 'database'])->name('install.database');
    Route::post('/database', [InstallerController::class, 'saveDatabase'])->name('install.database.save');
    Route::get('/admin', [InstallerController::class, 'admin'])->name('install.admin');
    Route::post('/admin', [InstallerController::class, 'saveAdmin'])->name('install.admin.save');
    Route::get('/finish', [InstallerController::class, 'finish'])->name('install.finish');
});

Route::get('/', function () {
    return view('welcome');
});

Route::get('/dashboard', [DashboardController::class, 'index'])
    ->middleware(['auth', 'verified'])
    ->name('dashboard');

Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');

    Route::get('/profile/two-factor', [App\Http\Controllers\TwoFactorController::class, 'show'])->name('two-factor.show');
    Route::post('/profile/two-factor/enable', [App\Http\Controllers\TwoFactorController::class, 'enable'])->name('two-factor.enable');
    Route::post('/profile/two-factor/confirm', [App\Http\Controllers\TwoFactorController::class, 'confirm'])->name('two-factor.confirm');
    Route::delete('/profile/two-factor', [App\Http\Controllers\TwoFactorController::class, 'disable'])->name('two-factor.disable');

    Route::get('/profile/api-tokens', [App\Http\Controllers\ApiTokensController::class, 'index'])->name('api-tokens.index');
    Route::post('/profile/api-tokens', [App\Http\Controllers\ApiTokensController::class, 'store'])->name('api-tokens.store');
    Route::delete('/profile/api-tokens/{token}', [App\Http\Controllers\ApiTokensController::class, 'destroy'])->name('api-tokens.destroy');
});

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/contacts/autocomplete', [ContactsController::class, 'autocomplete'])->name('contacts.autocomplete');
    Route::post('/contacts/enrich', [ContactsController::class, 'enrich'])->name('contacts.enrich');
    Route::post('/contacts/suggest-tags', [ContactsController::class, 'suggestTags'])->name('contacts.suggest-tags');
    Route::post('/contacts/bulk', [ContactsController::class, 'bulkAction'])->name('contacts.bulk');

    Route::get('/bulk-sends', [App\Http\Controllers\BulkSendsController::class, 'index'])->name('bulk-sends.index');
    Route::get('/bulk-sends/compose', [App\Http\Controllers\BulkSendsController::class, 'compose'])->name('bulk-sends.compose');
    Route::post('/bulk-sends', [App\Http\Controllers\BulkSendsController::class, 'store'])->name('bulk-sends.store');
    Route::delete('/bulk-sends/templates/{template}', [App\Http\Controllers\BulkSendsController::class, 'destroyTemplate'])->name('bulk-sends.templates.destroy');
    Route::get('/bulk-sends/{bulkSend}', [App\Http\Controllers\BulkSendsController::class, 'show'])->name('bulk-sends.show');
    Route::get('/contacts/import', [ImportsController::class, 'form'])->name('imports.form');
    Route::get('/contacts/import/template.csv', [ImportsController::class, 'template'])->name('imports.template');
    Route::post('/contacts/import/preview', [ImportsController::class, 'preview'])->name('imports.preview');
    Route::post('/contacts/import', [ImportsController::class, 'store'])->name('imports.store');
    Route::get('/contacts/{contact}/merge', [ContactsController::class, 'merge'])->name('contacts.merge');
    Route::post('/contacts/{contact}/merge', [ContactsController::class, 'mergeStore'])->name('contacts.merge.store');
    Route::resource('contacts', ContactsController::class);

    Route::get('/sms', [SmsController::class, 'index'])->name('sms.index');
    Route::post('/messaging/spell-check', [SmsController::class, 'spellCheck'])->name('messaging.spell-check');
    Route::post('/messaging/translate', [SmsController::class, 'translate'])->name('messaging.translate');
    Route::get('/sms/{contact}', [SmsController::class, 'show'])->name('sms.show');
    Route::post('/sms/{contact}', [SmsController::class, 'store'])->name('sms.store');

    Route::get('/calls', [CallsController::class, 'index'])->name('calls.index');
    Route::post('/calls/log', [CallsController::class, 'log'])->name('calls.log');
    Route::get('/calls/token', [CallsController::class, 'token'])->name('calls.token');

    Route::get('/emails', [EmailsController::class, 'index'])->name('emails.index');
    Route::get('/emails/create', [EmailsController::class, 'create'])->name('emails.create');
    Route::post('/emails', [EmailsController::class, 'store'])->name('emails.store');

    Route::get('/groups', [GroupsController::class, 'index'])->name('groups.index');
    Route::post('/groups', [GroupsController::class, 'store'])->name('groups.store');
    Route::patch('/groups/{group}', [GroupsController::class, 'update'])->name('groups.update');
    Route::delete('/groups/{group}', [GroupsController::class, 'destroy'])->name('groups.destroy');

    Route::get('/tags', [TagsController::class, 'index'])->name('tags.index');
    Route::post('/tags', [TagsController::class, 'store'])->name('tags.store');
    Route::patch('/tags/{tag}', [TagsController::class, 'update'])->name('tags.update');
    Route::delete('/tags/{tag}', [TagsController::class, 'destroy'])->name('tags.destroy');

    Route::get('/audit', [App\Http\Controllers\AuditController::class, 'index'])->name('audit.index');

    Route::get('/reminders/calendar', [App\Http\Controllers\RemindersController::class, 'calendar'])->name('reminders.calendar');
    Route::get('/reminders', [App\Http\Controllers\RemindersController::class, 'index'])->name('reminders.index');
    Route::post('/reminders', [App\Http\Controllers\RemindersController::class, 'store'])->name('reminders.store');
    Route::patch('/reminders/{reminder}/complete', [App\Http\Controllers\RemindersController::class, 'complete'])->name('reminders.complete');
    Route::delete('/reminders/{reminder}', [App\Http\Controllers\RemindersController::class, 'destroy'])->name('reminders.destroy');

    Route::get('/workspace/members', [WorkspaceController::class, 'members'])->name('workspace.members');
    Route::get('/workspace/export', [App\Http\Controllers\WorkspaceExportController::class, 'download'])->name('workspace.export');
    Route::post('/workspace/invite', [WorkspaceController::class, 'invite'])->name('workspace.invite');
    Route::delete('/workspace/invitations/{invitation}', [WorkspaceController::class, 'revokeInvitation'])->name('workspace.invitations.revoke');
    Route::delete('/workspace/members/{member}', [WorkspaceController::class, 'removeMember'])->name('workspace.members.remove');
    Route::post('/workspace/switch/{team}', [WorkspaceController::class, 'switchTeam'])->name('workspace.switch');
    Route::post('/invitations/{token}/accept', [WorkspaceController::class, 'acceptInvitation'])->name('invitations.accept.post');

    Route::get('/settings', [SettingsController::class, 'index'])->name('settings.index');
    Route::get('/settings/general', [SettingsController::class, 'general'])->name('settings.general');
    Route::patch('/settings/general', [SettingsController::class, 'updateGeneral'])->name('settings.general.update');

    Route::get('/settings/branding', [SettingsController::class, 'branding'])->name('settings.branding');
    Route::patch('/settings/branding', [SettingsController::class, 'updateBranding'])->name('settings.branding.update');

    Route::get('/settings/mail', [SettingsController::class, 'mail'])->name('settings.mail');
    Route::patch('/settings/mail', [SettingsController::class, 'updateMail'])->name('settings.mail.update');
    Route::post('/settings/mail/test', [SettingsController::class, 'sendTestMail'])->name('settings.mail.test');

    Route::get('/settings/twilio', [SettingsController::class, 'twilio'])->name('settings.twilio');
    Route::patch('/settings/twilio', [SettingsController::class, 'updateTwilio'])->name('settings.twilio.update');

    Route::get('/settings/ai', [SettingsController::class, 'ai'])->name('settings.ai');
    Route::patch('/settings/ai', [SettingsController::class, 'updateAi'])->name('settings.ai.update');
    Route::post('/settings/ai/test', [SettingsController::class, 'testAi'])->name('settings.ai.test');
});

Route::post('/webhooks/twilio/sms', [TwilioWebhookController::class, 'incomingSms'])
    ->middleware('twilio.signature')
    ->name('webhooks.twilio.sms');

Route::get('/track/email/{trackingId}.gif', [TrackingController::class, 'emailPixel'])
    ->where('trackingId', '[a-fA-F0-9-]+')
    ->name('tracking.email');

Route::get('/invitations/{token}', [WorkspaceController::class, 'showInvitation'])->name('invitations.accept');

require __DIR__.'/auth.php';
