<?php

namespace App\Http\Controllers;

use App\Models\Line;
use App\Models\Plant;
use Illuminate\Http\Request;

class ModulePlantLineController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    // public function get_modulePlantLine(Request $request)
    // {
    //     $expectedUser = env('API_AUTH_USER');
    //     $expectedPw   = env('API_AUTH_PW');

    //     $header_auth = $request->header('Authorization');
    //     $expectedToken = $expectedUser . ':' . $expectedPw;

    //     if ("Bearer " . $expectedToken != $header_auth)
    //     {
    //         return response()->json([
    //             'status_code' => 'ERROR',
    //             'status_description' => 'Invalid authorization token!'
    //         ], 403);
    //     }

    //     $plantName = $request->header('plant-name');

    //     if (empty($plantName))
    //     {
    //         return response()->json([
    //             'status_code' => 'ERROR',
    //             'status_description' => "Plant Name can't be empty!"
    //         ], 404);
    //     }

    //     $plant = Plant::where('name', $plantName)->first();

    //     if (!$plant) {
    //         return response()->json([
    //             'status_code' => 'ERROR',
    //             'status_description' => "Plant '{$plantName}' not found!"
    //         ], 404);
    //     }

    //     $lineNames = Line::where('plant_id', $plant->id)
    //         ->orderBy('created_at', 'asc')
    //         ->pluck('name');

    //     if ($lineNames->isEmpty()) {
    //         return response()->json([
    //             'status_code' => 'ERROR',
    //             'status_description' => "No lines found for plant '{$plantName}'"
    //         ], 404);
    //     }

    //     // Prepend 'All Lines'
    //     $lineNames->prepend('All Lines')->values();

    //     return response()->json([
    //         'status_code' => 'SUCCESS',
    //         'status_description' => $lineNames,
    //     ], 200);

    // }

    public function get_modulePlantLine(Request $request)
    {
        $expectedUser = env('API_AUTH_USER');
        $expectedPw   = env('API_AUTH_PW');

        $header_auth = $request->header('Authorization');
        $expectedToken = $expectedUser . ':' . $expectedPw;

        if ("Bearer " . $expectedToken != $header_auth) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => 'Invalid authorization token!'
            ], 403);
        }

        $plantName = $request->header('plant-name');
        $chartName = $request->header('chart-name');

        if (empty($plantName)) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => "Plant name can't be empty!"
            ], 404);
        }

        if (empty($chartName)) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => "Chart name can't be empty!"
            ], 404);
        }

        $plant = Plant::where('name', $plantName)->first();

        if (!$plant)
        {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => "Plant '{$plantName}' not found!"
            ], 404);
        }

        // Load only lines NOT equal to FG Line
        $nonFgLines = Line::where('plant_id', $plant->id)
            //->where('type', '!=', 'FG Line')
            ->orderBy('created_at', 'asc')
            ->pluck('name')
            ->toArray();

        if (empty($nonFgLines)) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => "No non-FG lines found for plant '{$plantName}'"
            ], 404);
        }

        $normalizedChartName = strtolower(trim($chartName));

        if ($normalizedChartName != 'production order count' && $normalizedChartName != 'production hourly count' && $normalizedChartName != 'production line stop count') {
            array_unshift($nonFgLines, 'All Lines');
        }


        // if (strtolower(trim($chartName)) != 'production hourly count') {
        //     array_unshift($nonFgLines, 'All Lines');
        // }

        // Add "All Lines" to beginning and "FG Lines" at the end
       // array_unshift($nonFgLines, 'All Lines');
       // $nonFgLines[] = 'FG Line';

        return response()->json([
            'status_code' => 'SUCCESS',
            'status_description' => $nonFgLines,
        ], 200);
    }


    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }
}
