<?php
use Illuminate\Support\Facades\Route;

use App\Http\Controllers\HomeController;
use App\Http\Controllers\ProductController;
use App\Http\Controllers\PageController;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\VideoController;
use App\Http\Controllers\DemoController;
use App\Http\Controllers\SitemapController;
use App\Http\Controllers\CollectionController;

use App\Models\DistProductCategory;
use App\Models\SitePage;
use App\Models\DistAdminDistributor;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

//巴西官网专用路由
$host = request()->getHttpHost();    // 安全且可靠
$uri = request()->getRequestUri();
$brHost = env('BR_HOST'); // 巴西官网域名
if ($host == $brHost) {
    $segments = explode('/', $uri);
    if ($segments) {
        $slug = $segments[1];
        if ($slug == 'produto') {
            Route::get('/produto/{slug}', [ProductController::class, 'detail'])->name('products.detail');
        } else {
            $distInfo = DistAdminDistributor::findByDomain($brHost);
            $categoryId = DistProductCategory::where(function ($query) use ($slug) {
                $query->where('slug', $slug);
            })
                ->where('dist_id', $distInfo->id)
                ->first();
            if ($categoryId) {
                return Route::get('/{categoryId}', [ProductController::class, 'category'])->name('products.category');
            } else {
                $page = SitePage::where('status', '1')->where('dist_id', $distInfo->id)->where('slug', $slug)->first();
                if ($page) {
                    return Route::get('/{slug}', [PageController::class, 'detail'])->name('page.detail');
                }
            }
        }
    }
}





Route::get('/demo', [DemoController::class, 'index'])->name('demo');


// 主页
Route::get('/', [HomeController::class, 'index'])->name('home');

// 产品路由
Route::prefix('products')->group(function () {
    Route::get('/', [ProductController::class, 'index'])->name('products.index');
    Route::get('/{id}', [ProductController::class, 'detail'])->name('products.detail');
    Route::get('/categories/{categoryId}', [ProductController::class, 'category'])->name('products.category');
});

// 视频路由
Route::prefix('videos')->group(function () {
    Route::get('/', [VideoController::class, 'index'])->name('videos.index');
    Route::get('/{id}', [VideoController::class, 'detail'])->name('videos.detail');
    Route::get('/categories/{categoryId}', [VideoController::class, 'category'])->name('videos.category');
});

// 文章路由
Route::prefix('pages')->group(function () {
    Route::get('/', [PageController::class, 'list'])->name('page.list'); // 页面列表页
    Route::get('/{slug}', [PageController::class, 'detail'])->name('page.detail'); // 页面详情页
});

////专题路由,于用文章
//Route::get('/collections/{slug}', [CollectionController::class, 'show'])->name('collections.show');
//

// 询盘路由
Route::prefix('contact')->group(function () {
    Route::get('/', [ContactController::class, 'create'])->name('contact.create');
    Route::post('/', [ContactController::class, 'store'])->name('contact.store');
});

Route::prefix('collections')->group(function () {
    Route::get('/{slug}', [CollectionController::class, 'detail'])->name('collection.detail');
});

// Sitemap 路由
Route::get('/sitemap.xml', [SitemapController::class, 'index'])->name('sitemap.index');