<?php

namespace App\Http\Controllers;

use App\Http\Resources\CompanyResource;
use App\Models\company;
use Illuminate\Http\Request;

class CompanyController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return company::with("parent_info")->get();

    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        $company = new company();
        $company->plant = $request->plant;
        $company->building = $request->building;
        $company->code = $request->code;
        $company->parent = $request->parent;
        $company->description = $request->description;
        $company->save();
        return response()->json(// todo : see macro vidoes
            [
                "data" => $company,
                "message" => "success",
                "status" => true,
            ]
        );
        // return CompanyResource::collection(company::all());
    }

    /**
     * Display the specified resource.
     */
    public function show(company $company, $id)
    {
        try{
        return response()->json(// todo : see macro vidoes
            [
                "data" => new CompanyResource(company::find($id)),
                "message" => "success",
                "status" => true,
            ]
        );
        }
        catch (\Throwable $th) {
        return response()->json(// todo : see macro vidoes
            [
                "data" => [],
                "message" => "No Record Found !",
                "status" => false,
            ]
            );
    }
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(company $company)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, $id)
    {
        $company = company::find($id);
        $company->code = $request->Company_Code;
        $company->plant = $request->plant_name;
        $company->parent = $request->parent_name;
        $company->building = $request->building_name;
        $company->description = $request->description_text;
        $company->save();
        return response()->json(// todo : see macro vidoes
            [
                "data" => new CompanyResource(company::find($id)),
                "message" => "success",
                "status" => true,
            ]
        );
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(company $company)
    {
        //
    }
    public function getCompaniesByParents(Request $request, $parent_id = 0)
    {
        $company = company::where("parent", $parent_id)->get();
        return response()->json(// todo : see macro vidoes
            [
                "data" => CompanyResource::collection($company),
                "message" => "success",
                "status" => true,
            ]
        );
    }
}
