<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class TerbaruController extends Controller
{
    // ============================================================
    // FUNGSI GUDANG DATA (Hanya untuk menyimpan array)
    // ============================================================
    private function getDataProduk()
    {
        $apparelNew = [
            [
                'name'        => 'Stone Island Polo Shirt',
                'category'    => 'Apparel New',
                'status'      => 'NEW',
                'price'       => '1.250.000',
                'image'       => ['an-1-a.png', 'an-1-b.png', 'an-1-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['S', 'M', 'L', 'Xl'],
                'upload_date' => '2026-04-30 14:30:00',
                'stock'       => 3,
                'views'       => 2847,
            ],
            [
                'name'        => 'Tracktop BW Bird',
                'category'    => 'Apparel New',
                'status'      => 'NEW',
                'price'       => '1.350.000',
                'image'       => ['an-2-a.png', 'an-2-b.png', 'an-2-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['M', 'L'],
                'upload_date' => '2026-04-28 10:15:00',
                'stock'       => 5,
                'views'       => 1563,
            ],
            [
                'name'        => 'Tracktop Ellese Black',
                'category'    => 'Apparel New',
                'status'      => 'NEW',
                'price'       => '1.650.000',
                'image'       => ['an-3-a.png', 'an-3-b.png', 'an-3-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['L', 'Xl', 'Xxl'],
                'upload_date' => '2026-04-25 16:45:00',
                'stock'       => 2,
                'views'       => 2190,
            ],
            [
                'name'        => 'Jersey Adidas Oasis',
                'category'    => 'Apparel New',
                'status'      => 'NEW',
                'price'       => '2.250.000',
                'image'       => ['an-4-a.png', 'an-4-b.png', 'an-4-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['Xs', 'S', 'M'],
                'upload_date' => '2026-04-22 09:20:00',
                'stock'       => 4,
                'views'       => 874,
            ],
            [
                'name'        => 'Jaket Napapijri',
                'category'    => 'Apparel New',
                'status'      => 'NEW',
                'price'       => '3.150.000',
                'image'       => ['an-5-a.png', 'an-5-b.png', 'an-5-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['Xl', 'Xxl', '3xl'],
                'upload_date' => '2026-04-18 11:05:00',
                'stock'       => 1,
                'views'       => 431,
            ],
            [
                'name'        => 'Rugby Adidas',
                'category'    => 'Apparel New',
                'status'      => 'NEW',
                'price'       => '950.000',
                'image'       => ['an-6-a.png', 'an-6-b.png', 'an-6-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['M', 'L', '14'],
                'upload_date' => '2026-04-10 08:30:00',
                'stock'       => 3,
                'views'       => 1208,
            ],
        ];

        $apparelSecond = [
            [
                'name'        => 'Adidas Ringertee Camo',
                'category'    => 'Apparel Second',
                'status'      => 'SECOND',
                'price'       => '150.000',
                'image'       => ['as-1-a.png', 'as-1-b.png', 'as-1-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['M'],
                'upload_date' => '2026-04-30 08:15:22',
                'stock'       => 1,
                'views'       => 1847,
            ],
            [
                'name'        => 'Adidas Big Logo',
                'category'    => 'Apparel Second',
                'status'      => 'SECOND',
                'price'       => '120.000',
                'image'       => ['as-2-a.png', 'as-2-b.png', 'as-2-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['Xl'],
                'upload_date' => '2026-04-28 14:30:05',
                'stock'       => 1,
                'views'       => 634,
            ],
            [
                'name'        => 'Jaket TNF',
                'category'    => 'Apparel Second',
                'status'      => 'SECOND',
                'price'       => '450.000',
                'image'       => ['as-3-a.png', 'as-3-b.png', 'as-3-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['L'],
                'upload_date' => '2026-04-26 10:45:10',
                'stock'       => 1,
                'views'       => 2315,
            ],
            [
                'name'        => 'Stone Island Anora Multi',
                'category'    => 'Apparel Second',
                'status'      => 'SECOND',
                'price'       => '600.000',
                'image'       => ['as-4-a.png', 'as-4-b.png', 'as-4-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['Xxl'],
                'upload_date' => '2026-04-22 19:20:45',
                'stock'       => 1,
                'views'       => 981,
            ],
            [
                'name'        => 'Tracktop Adidas Grey',
                'category'    => 'Apparel Second',
                'status'      => 'SECOND',
                'price'       => '250.000',
                'image'       => ['as-5-a.png', 'as-5-b.png', 'as-5-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['S'],
                'upload_date' => '2026-04-15 16:10:30',
                'stock'       => 1,
                'views'       => 1452,
            ],
            [
                'name'        => 'Adidas Logo Camo',
                'category'    => 'Apparel Second',
                'status'      => 'SECOND',
                'price'       => '130.000',
                'image'       => ['as-6-a.png', 'as-6-b.png', 'as-6-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['M'],
                'upload_date' => '2026-04-10 09:05:15',
                'stock'       => 1,
                'views'       => 276,
            ],
        ];

        $footwearNew = [
            [
                'name'        => 'Evo SL Core Black',
                'category'    => 'Footwear New',
                'status'      => 'NEW',
                'price'       => '2.650.000',
                'image'       => ['fn-1-a.png', 'fn-1-b.png', 'fn-1-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['40', '41', '42', '43'],
                'upload_date' => '2026-05-01 10:15:30',
                'stock'       => 4,
                'views'       => 2963,
            ],
            [
                'name'        => 'Adidas Spezial Ice Blue',
                'category'    => 'Footwear New',
                'status'      => 'NEW',
                'price'       => '2.350.000',
                'image'       => ['fn-2-a.png', 'fn-2-b.png', 'fn-2-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['39', '40', '44'],
                'upload_date' => '2026-04-29 14:20:15',
                'stock'       => 2,
                'views'       => 1784,
            ],
            [
                'name'        => 'Adidas Samba Classic Black',
                'category'    => 'Footwear New',
                'status'      => 'NEW',
                'price'       => '1.850.000',
                'image'       => ['fn-3-a.png', 'fn-3-b.png', 'fn-3-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['41', '42', '45'],
                'upload_date' => '2026-04-27 09:45:00',
                'stock'       => 5,
                'views'       => 2201,
            ],
            [
                'name'        => 'Adidas Samba Classic White',
                'category'    => 'Footwear New',
                'status'      => 'NEW',
                'price'       => '1.950.000',
                'image'       => ['fn-4-a.png', 'fn-4-b.png', 'fn-4-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['38', '39', '40'],
                'upload_date' => '2026-04-25 16:10:45',
                'stock'       => 1,
                'views'       => 548,
            ],
            [
                'name'        => 'Adidas Spezial Cw Oslo',
                'category'    => 'Footwear New',
                'status'      => 'NEW',
                'price'       => '3.150.000',
                'image'       => ['fn-5-a.png', 'fn-5-b.png', 'fn-5-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['42', '43', '44'],
                'upload_date' => '2026-04-21 11:30:20',
                'stock'       => 3,
                'views'       => 1129,
            ],
            [
                'name'        => 'Adidas Series Bali',
                'category'    => 'Footwear New',
                'status'      => 'NEW',
                'price'       => '3.150.000',
                'image'       => ['fn-6-a.png', 'fn-6-b.png', 'fn-6-c.png'],
                'badge_class' => 'badge-new',
                'sizes'       => ['40', '41', '42'],
                'upload_date' => '2026-04-18 08:05:10',
                'stock'       => 2,
                'views'       => 317,
            ],
        ];

        $footwearSecond = [
            [
                'name'        => 'Adidas LA Trainer',
                'category'    => 'Footwear Second',
                'status'      => 'SECOND',
                'price'       => '650.000',
                'image'       => ['fs-1-a.png', 'fs-1-b.png', 'fs-1-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['41'],
                'upload_date' => '2026-05-01 15:30:00',
                'stock'       => 1,
                'views'       => 2741,
            ],
            [
                'name'        => 'Adidas Samba OG',
                'category'    => 'Footwear Second',
                'status'      => 'SECOND',
                'price'       => '550.000',
                'image'       => ['fs-2-a.png', 'fs-2-b.png', 'fs-2-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['40'],
                'upload_date' => '2026-04-30 11:20:15',
                'stock'       => 1,
                'views'       => 1093,
            ],
            [
                'name'        => 'Adidas Spezial Orbit Indigo',
                'category'    => 'Footwear Second',
                'status'      => 'SECOND',
                'price'       => '600.000',
                'image'       => ['fs-3-a.png', 'fs-3-b.png', 'fs-3-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['43'],
                'upload_date' => '2026-04-29 09:45:10',
                'stock'       => 1,
                'views'       => 1876,
            ],
            [
                'name'        => 'Adidas TRX Vintage',
                'category'    => 'Footwear Second',
                'status'      => 'SECOND',
                'price'       => '950.000',
                'image'       => ['fs-4-a.png', 'fs-4-b.png', 'fs-4-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['42'],
                'upload_date' => '2026-04-27 16:15:45',
                'stock'       => 1,
                'views'       => 462,
            ],
            [
                'name'        => 'Adidas Dragon',
                'category'    => 'Footwear Second',
                'status'      => 'SECOND',
                'price'       => '350.000',
                'image'       => ['fs-5-a.png', 'fs-5-b.png', 'fs-5-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['39'],
                'upload_date' => '2026-04-25 14:10:30',
                'stock'       => 1,
                'views'       => 2158,
            ],
            [
                'name'        => 'Adidas Munchen Grey',
                'category'    => 'Footwear Second',
                'status'      => 'SECOND',
                'price'       => '750.000',
                'image'       => ['fs-6-a.png', 'fs-6-b.png', 'fs-6-c.png'],
                'badge_class' => 'badge-second',
                'sizes'       => ['44'],
                'upload_date' => '2026-04-23 08:05:15',
                'stock'       => 1,
                'views'       => 729,
            ],
        ];
// Langsung gabungkan dan kembalikan datanya
        return array_merge($apparelNew, $apparelSecond, $footwearNew, $footwearSecond);
    }


    // ============================================================
    // FUNGSI UNTUK HALAMAN BERANDA (MENGAMBIL 8 TERBARU)
    // ============================================================
    public function index()
    {
        // 1. Panggil data dari fungsi gudang di atas
        $allProducts = $this->getDataProduk();

        // 2. Urutkan berdasarkan tanggal terbaru
        usort($allProducts, fn($a, $b) => strtotime($b['upload_date']) <=> strtotime($a['upload_date']));

        // 3. Potong 8 saja
        $latestProducts = array_slice($allProducts, 0, 8);

        return view('welcome', compact('latestProducts'));
    }


    // ============================================================
    // FUNGSI UNTUK HALAMAN DETAIL PRODUK
    // ============================================================
    public function show($kategori, $nama_produk)
{
    // 1. Panggil data dari fungsi gudang (getDataProduk)
    $allProducts = $this->getDataProduk();

    // 2. Cari produk berdasarkan namanya
    $produkYangDicari = null;
    
    foreach ($allProducts as $item) {
        // Kita bandingkan nama produk di URL dengan nama di data gudang
        if ($item['name'] === $nama_produk) {
            $produkYangDicari = $item;
            break; 
        }
    }

    // 3. Jika tidak ketemu, lempar error 404
    if (!$produkYangDicari) {
        abort(404, 'Produk tidak ditemukan'); 
    }

    // 4. Kirim data produk ke view 'detailproduk'
    return view('detailproduk', [
        'product' => $produkYangDicari
    ]);
}
}