<?php

namespace App\Http\Controllers;

use App\Models\EquipmentMaster;
use App\Models\Machine;
use App\Models\Plant;
use Illuminate\Http\Request;

class EquipmentMasterController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function equipmentMasterData(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');

        $workCenter = $request->header('work-center');

        if (!$plantCode) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => "Plant Code value can't be empty"
            ], 404);
        }
        else if (!$workCenter) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => "Work Center value can't be empty"
            ], 404);
        }

        $plant = Plant::where('code', $plantCode)->first();

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

        $plantId = $plant->id;

        $machine = Machine::where('work_center', $workCenter)->first();

        $machinePlant = Machine::where('work_center', $workCenter)
        ->where('plant_id', $plantId)
        ->first();

        if (!$machine)
        {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => "work center '{$workCenter}' not found!"
            ], 404);
        }

        if (!$machinePlant) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => "Work center '{$workCenter}' not found for the plant code '{$plant->code}'!"
            ], 404);
        }

        $machineId = $machine->id;

        $machineExists = EquipmentMaster::where('plant_id', $plantId)
        ->where('machine_id', $machineId)
        ->first();

        if (!$machineExists) {
            return response()->json([
                'status_code' => 'ERROR',
                'status_description' => "Work center '{$machine->work_center}' not found for the plant code '{$plant->code}' in Equipment Master!"
            ], 404);
        }

        $equipments = EquipmentMaster::where('plant_id', $plant->id)
            ->where('machine_id', $machineId)
            ->get([
                // 'name',
                'make',
                'model',
                'equipment_number',
                'calibrated_on',
                'next_calibration_date',
                'calibrated_by',
            ]);


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