<?php

namespace App\Http\Controllers;

use App\Models\ModuleList;
use Illuminate\Http\Request;

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

    public function get_moduleFilter(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);
        }

        $lineName = $request->header('line-name');

        if (empty($lineName))
        {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => "Line name can't be empty!"
            ], 404);
        }
          // If 'line-name' is 'FG Lines', return actual FG Line names
        if (strtolower(trim($lineName)) == 'fg lines') {
            $fgLines = \App\Models\Line::where('type', 'FG Line')
                ->orderBy('created_at', 'asc')
                ->pluck('name');

            if ($fgLines->isEmpty()) {
                return response()->json([
                    'status_code' => 'ERROR',
                    'status_description' => 'No FG Lines found!'
                ], 404);
            }

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

        // Default logic to return filter names
        $filterNames = ModuleList::orderBy('created_at', 'asc')
            ->get()
            ->unique('filter_name')
            ->pluck('filter_name')
            ->filter()
            ->values();

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

        // $filterNames = ModuleList::orderBy('created_at', 'asc')
        //     ->get()
        //     ->unique('filter_name')
        //     ->pluck('filter_name')
        //     ->filter()
        //     ->values();

        //     return response()->json([
        //     'status_code' => 'SUCCESS',
        //     'status_description' => $filterNames
        // ], 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)
    {
        //
    }
}
