<?php

namespace App\Http\Controllers;

use App\Models\Training;
use App\Models\Brand;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class TrainingController extends Controller
{
    public function index(Request $request)
    {
        // Ambil semua brand dari tabel brands untuk dropdown
        $brands = Brand::all();

        // Ambil brand yang dipilih dari query parameter (default ke ID pertama)
        $brand = $request->input('brand', $brands->first()->id ?? 1);

        // Ambil data training sesuai brand
        $trainings = Training::where('brands', $brand)->orderBy('no')->get();

        // Cek apakah ada data training sama sekali
        $hasData = $trainings->isNotEmpty();

        return view('training', compact('trainings', 'brand', 'brands', 'hasData'));
    }

    // public function generate(Request $request)
    // {
    //     // Mengambil data dari tabel samples
    //     DB::insert("
    //     INSERT INTO training (no, data_1, data_2, data_3, target, brands)
    //     SELECT
    //         ROW_NUMBER() OVER (PARTITION BY s1.brand ORDER BY s1.id) AS no,
    //         s1.data AS data_1,
    //         s2.data AS data_2,
    //         s3.data AS data_3,
    //         s4.data AS target,
    //         s1.brand AS brands
    //     FROM samples s1
    //     JOIN samples s2 ON s2.id = s1.id + 1
    //     JOIN samples s3 ON s3.id = s1.id + 2
    //     JOIN samples s4 ON s4.id = s1.id + 3
    //     WHERE s1.brand BETWEEN 1 AND 11
    //       AND s2.brand BETWEEN 1 AND 11
    //       AND s3.brand BETWEEN 1 AND 11
    //       AND s4.brand BETWEEN 1 AND 11
    //       AND s1.bulan BETWEEN 1 AND 8
    //       AND s2.bulan BETWEEN 1 AND 8
    //       AND s3.bulan BETWEEN 1 AND 8
    //       AND s4.bulan BETWEEN 1 AND 8
    //     ORDER BY s1.brand, s1.id
    // ");
    //     return redirect()->route('training.index')->with('success', 'Data Training berhasil diambil.');
    // }







    public function generate(Request $request)
{
    $startMonth = session('startMonth'); // Ambil dari session

    // Buat array bulan untuk 8 bulan ke depan
    $validMonths = [];
    for ($i = 0; $i < 8; $i++) {
        $month = ($startMonth + $i - 1) % 12 + 1;
        $validMonths[] = $month;
    }

    // Ambil semua brand_id aktif
    $brands = DB::table('products')
        ->where('status', 'aktif')
        ->distinct()
        ->pluck('brand_id')
        ->toArray(); // ubah jadi array biasa untuk implode

    if (empty($brands)) {
        return redirect()->route('training.index')->with('error', 'Tidak ada brand aktif ditemukan.');
    }

    // Jalankan query insert
    DB::insert("
        INSERT INTO training (no, data_1, data_2, data_3, target, brands)
        SELECT
            ROW_NUMBER() OVER (PARTITION BY s1.brand ORDER BY s1.id) AS no,
            s1.data AS data_1,
            s2.data AS data_2,
            s3.data AS data_3,
            s4.data AS target,
            s1.brand AS brands
        FROM samples s1
        JOIN samples s2 ON s2.id = s1.id + 1
        JOIN samples s3 ON s3.id = s1.id + 2
        JOIN samples s4 ON s4.id = s1.id + 3
        WHERE 
            s1.brand IN (" . implode(',', $brands) . ")
            AND s2.brand IN (" . implode(',', $brands) . ")
            AND s3.brand IN (" . implode(',', $brands) . ")
            AND s4.brand IN (" . implode(',', $brands) . ")
            AND s1.bulan IN (" . implode(',', $validMonths) . ")
            AND s2.bulan IN (" . implode(',', $validMonths) . ")
            AND s3.bulan IN (" . implode(',', $validMonths) . ")
            AND s4.bulan IN (" . implode(',', $validMonths) . ")
        ORDER BY s1.brand, s1.id
    ");

    return redirect()->route('training.index')->with('success', 'Data Training berhasil diambil.');
}


    //     public function generate(Request $request)
    // {
    //     $startMonth = (int) $request->input('bulan'); // misal Maret = 3
    //     $endMonth = $startMonth + 8;

    //     DB::table('training')->truncate();

    //     DB::insert("
    //         INSERT INTO training (no, data_1, data_2, data_3, target, brands)
    //         SELECT
    //             ROW_NUMBER() OVER (PARTITION BY s1.brand ORDER BY s1.id) AS no,
    //             s1.data AS data_1,
    //             s2.data AS data_2,
    //             s3.data AS data_3,
    //             s4.data AS target,
    //             s1.brand AS brands
    //         FROM samples s1
    //         JOIN samples s2 ON s2.id = s1.id + 1
    //         JOIN samples s3 ON s3.id = s1.id + 2
    //         JOIN samples s4 ON s4.id = s1.id + 3
    //         WHERE s1.brand BETWEEN 1 AND 11
    //           AND s2.brand BETWEEN 1 AND 11
    //           AND s3.brand BETWEEN 1 AND 11
    //           AND s4.brand BETWEEN 1 AND 11
    //           AND s1.bulan BETWEEN ? AND ?
    //           AND s2.bulan BETWEEN ? AND ?
    //           AND s3.bulan BETWEEN ? AND ?
    //           AND s4.bulan BETWEEN ? AND ?
    //           AND s1.data IS NOT NULL AND s2.data IS NOT NULL AND s3.data IS NOT NULL AND s4.data IS NOT NULL
    //         ORDER BY s1.brand, s1.id
    //     ", [
    //         $startMonth, $endMonth,
    //         $startMonth, $endMonth,
    //         $startMonth, $endMonth,
    //         $startMonth, $endMonth
    //     ]);

    //     return redirect()->route('training.index')->with('success', 'Data Training berhasil diambil.');
    // }

    public function reset()
    {
        DB::table('training')->truncate();

        return redirect()->route('training.index')->with('success', 'Data Training berhasil direset.');
    }
}
