<?php

namespace App\Http\Controllers;

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

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

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

    /**
     * Display the specified resource.
     */
    public function get_data(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);
        }

        $plantCode = $request->header('plant-code');

        if ($plantCode == null || $plantCode == '') {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => "Plant code can't be empty!",
            ], 400);
        } elseif (Str::length($plantCode) < 4 || ! is_numeric($plantCode) || ! preg_match('/^[1-9]\d{3,}$/', $plantCode)) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => 'Invalid plant code found!',
            ], 400);
        }

        $plant = Plant::where('code', $plantCode)->first();
        if (! $plant) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => "Plant Code '{$plantCode}' not found!",
            ], 400);
        }

        $plantId = $plant->id;

        $lines = Line::where('plant_id', $plantId)->get();
        $linesData = $lines->map(function ($line) {
            return [
                'name' => $line->name,
                'type' => $line->type,
                'no_of_operations' => (string) $line->no_of_operation,
            ];
        });

        return response()->json([
            'lines' => $linesData,
        ]);
    }

    /**
     * Display the all resource.
     */
    public function get_all_data(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);
        }

        $lines = Line::with('plant')->orderBy('plant_id')->get();
        $linesData = $lines->map(function ($line) {
            return [
                'plant' => $line->plant ? $line->plant->name : '', // Get plant name
                'name' => $line->name,
                'type' => $line->type,
                'no_of_operations' => (string) $line->no_of_operation,
            ];
        });

        return response()->json([
            'lines' => $linesData,
        ]);
    }

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

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