<?php

namespace App\Http\Controllers;

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

class supplierController extends Controller
{
    public function store(Request $request){
        $supplier = new suppliers;
        $supplier->item_id  = $request->item_id ;
        $supplier->supplierName = $request->supplierName;
        $supplier->supplierAddress = $request->supplierAddress;
        $supplier->supplierTelephone = $request->supplierTelephone;
        $supplier->supplierMobile = $request->supplierMobile;
        $supplier->supplierEmail = $request->supplierEmail;
        $supplier->save();
        
        return response()->json([
            'message' => 'Supplier created successfully',
            'supplier' => $supplier
        ], 201);
    }

    public function index(){
        $suppliers = suppliers::select("id","item_id","supplierName","supplierAddress","supplierTelephone","supplierMobile","supplierEmail")
        ->with(['items' => function ($query) {
            $query->select('id', 'item_name'); // Only select the necessary fields from the company table
        }]) ->get();
        return response()->json($suppliers);
    }

    public function update(Request $request, $id){
        $supplier = suppliers::find($id);
        $supplier->item_id  = $request->item_id ;
        $supplier->supplierName = $request->supplierName;
        $supplier->supplierAddress = $request->supplierAddress;
        $supplier->supplierTelephone = $request->supplierTelephone;
        $supplier->supplierMobile = $request->supplierMobile;
        $supplier->supplierEmail = $request->supplierEmail;
        $supplier->save();
        
        return response()->json([
            'message' => 'Supplier updated successfully',
            'supplier' => $supplier
        ], 201);
    }   
}
