<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class DashboardController extends Controller
{
    // ============================================================
    // 1. GUDANG DATA PRODUK (Fungsi Privat)
    // ============================================================
    private function getDataProduk()
    {
        $apparelNew = [
            [
                'name' => 'Stone Island Polo Shirt',
                'category' => 'Apparel New',
                'status' => 'NEW',
                'description' => '100% Original',
                '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',
                'description' => '100% Original',
                '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',
                'description' => '100% Original',
                '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',
                'description' => '100% Original',
                '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',
                'description' => '100% Original',
                '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',
                'description' => '100% Original',
                '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',
                'description' => 'Tidak ada minus',
                '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',
                'description' => 'Tidak ada minus',
                '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',
                'description' => 'Tidak ada minus',
                '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',
                'description' => 'Tidak ada minus',
                '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',
                'description' => 'Tidak ada minus',
                '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',
                'description' => 'Tidak ada minus',
                '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',
                'description' => '100% Original',
                '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',
                'description' => '100% Original',
                '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',
                'description' => '100% Original',
                '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',
                'description' => '100% Original',
                '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',
                'description' => '100% Original',
                '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',
                'description' => '100% Original',
                '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',
                'description' => 'Tidak ada minus',
                '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',
                'description' => 'Tidak ada minus',
                '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',
                'description' => 'Tidak ada minus',
                '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',
                'description' => 'Tidak ada minus',
                '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',
                'description' => 'Tidak ada minus',
                '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',
                'description' => 'Tidak ada minus',
                '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,
            ],
        ];

        return array_merge($apparelNew, $apparelSecond, $footwearNew, $footwearSecond);
    }

    // ============================================================
    // 2. FUNGSI HALAMAN UTAMA DASHBOARD (Statistik)
    // ============================================================
    public function index()
    {
        $allProducts = $this->getDataProduk();

        $totalProduk = count($allProducts);
        $totalDilihat = array_sum(array_column($allProducts, 'views'));
        $totalStok = array_sum(array_column($allProducts, 'stock'));
        $totalKategori = count(array_unique(array_column($allProducts, 'category')));

        usort($allProducts, fn($a, $b) => $b['views'] <=> $a['views']);
        $topProducts = array_slice($allProducts, 0, 5);

        return view('dashboard', compact(
            'totalProduk',
            'totalDilihat',
            'totalStok',
            'totalKategori',
            'topProducts'
        ));
    }

    // ============================================================
    // 3. FUNGSI HALAMAN KELOLA PRODUK (CRUD Tabel)
    // ============================================================
    public function kelolaProduk()
    {
        $products = $this->getDataProduk();

        $productsForView = array_map(function ($item) {
            $item['image'] = is_array($item['image']) ? $item['image'][0] : $item['image'];
            return $item;
        }, $products);

        return view('kelolaproduk', ['products' => $productsForView]);
    }

    public function editProduk($name)
    {
        $allProducts = $this->getDataProduk();
        $nameDecoded = urldecode($name);
        $product = collect($allProducts)->firstWhere('name', $nameDecoded);

        if (!$product) {
            return redirect('/kelola-produk');
        }

        return view('editproduk', compact('product'));
    }

    // ============================================================
    // 4. FUNGSI HALAMAN TAMBAH PRODUK
    // ============================================================
    public function tambahProduk()
    {
        // Langsung arahkan ke view tambahproduk.blade.php
        return view('tambahproduk');
    }

    // ============================================================
    // 5. FUNGSI ACTION FORM (Bisa diisi nanti jika sudah pakai Database)
    // ============================================================
    public function storeProduk(Request $request)
    {
        // Logika untuk menyimpan produk baru ke database
        // ...
        
        return redirect('/kelola-produk')->with('success', 'Produk berhasil ditambahkan!');
    }

    public function updateProduk(Request $request, $name)
    {
        // Logika untuk mengupdate produk yang ada di database
        // ...
        
        return redirect('/kelola-produk')->with('success', 'Produk berhasil diperbarui!');
    }
    
    // ============================================================
    // 6. FUNGSI HALAMAN ANALITIK PRODUK
    // ============================================================
    public function analitikProduk()
    {
        // 1. Ambil semua data produk dummy dan jadikan Collection agar mudah difilter
        $allProducts = collect($this->getDataProduk());

        // 2. Hitung Stok Menipis (produk yang stoknya di bawah 3)
        $stokMenipisCount = $allProducts->where('stock', '<', 3)->count();

        // 3. Data Dummy Tambahan
        $rasioKlikWA = 19.5;
        $totalKeranjang = 28;

        // 4. Proses Data Per Kategori menggunakan Looping agar kode lebih ringkas
        $categories = [
            'Apparel New', 
            'Apparel Second', 
            'Footwear New', 
            'Footwear Second'
        ];

        $categoryStats = [];

        foreach ($categories as $cat) {
            // Ambil produk yang sesuai kategori saja
            $filtered = $allProducts->where('category', $cat);
            
            $categoryStats[$cat] = [
                'total_produk' => $filtered->count(), // Menghitung "X Produk Terdaftar"
                'total_views'  => $filtered->sum('views') // Menjumlahkan semua views dalam kategori ini
            ];
        }

        // 5. Cari views tertinggi untuk patokan 100% lebar Progress Bar di Blade
        $maxViews = max(array_column($categoryStats, 'total_views')) ?: 1;

        // 6. Kirim semua data ke view 'analitikproduk'
        return view('analitikproduk', compact(
            'categoryStats', 
            'maxViews', 
            'stokMenipisCount', 
            'rasioKlikWA', 
            'totalKeranjang'
        ));
    }
}