<?php

namespace App\Http\Controllers;

use App\Models\vendor;
use Dotenv\Exception\ValidationException;
use Exception;
use Illuminate\Http\Request;
use Validator;

class VendorController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        return vendor::all();
    }

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

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        try {
            $validator = Validator::make($request->all(), [
                'vname' => 'required|string|max:30',
                'address' => 'required|string|max:30',
                'contact_name' => 'required|string|max:30',
                'contact_no' => 'required|string|max:15',
                'email' => 'required|email|max:50',
                'phone' => 'nullable|string|max:15',
                'description' => 'nullable|string|max:500',
            ],[
            'vname.required' => 'Vendor name is required.',
            'vname.string' => 'Vendor name must be a string.',
            'vname.max' => 'Vendor name cannot exceed 30 characters.',
            'address.required' => 'Address is required.',
            'address.string' => 'Address must be a string.',
            'address.max' => 'Address cannot exceed 30 characters.',
            'contact_name.required' => 'Contact name is required.',
            'contact_name.string' => 'Contact name must be a string.',
            'contact_name.max' => 'Contact name cannot exceed 30 characters.',
            'contact_no.required' => 'Contact number is required.',
            'contact_no.string' => 'Contact number must be a string.',
            'contact_no.max' => 'Contact number cannot exceed 15 characters.',
            'email.required' => 'Email is required.',
            'email.email' => 'Email must be a valid email address.',
            'email.max' => 'Email cannot exceed 50 characters.',
            'phone.string' => 'Phone must be a string.',
            'phone.max' => 'Phone cannot exceed 15 characters.',
            'description.string' => 'Description must be a string.',
            'description.max' => 'Description cannot exceed 500 characters.',
        ]);

            $vendor = new vendor();
            $vendor->vname = $request->vname;
            $vendor->address = $request->address;
            $vendor->contact_name = $request->contact_name;
            $vendor->contact_no = $request->contact_no;
            $vendor->email = $request->email;
            $vendor->phone = $request->phone;
            $vendor->description = $request->description;
            $vendor->save();
            return response()->json(
                [
                    "data" => $vendor,
                    "message" => ["saved"],
                    "status" => true,
                ]
            );

        } catch (IlluminateValidationValidationException $e) {
            // مدیریت خطاهای ولیدیشن
            return response()->json([
                'errors' => $e->validator->errors(),
            ], 422);
        } catch (Exception $e) {
            // مدیریت سایر خطاها
            return response()->json([
                'error' => 'Something went wrong, please try again.',
                'message' => $e->getMessage(),
            ], 500);
        }
    }

    /**
     * Display the specified resource.
     */
    public function show(vendor $vendor, $id)
    {
        $vendor = Vendor::find($id);

        // بررسی اینکه آیا فروشنده وجود دارد یا خیر
        if (!$vendor) {
            return response()->json([
                'error' => 'Vendor not found.'
            ], 404);
        }

        // بازگرداندن اطلاعات فروشنده
        return response()->json(// todo : see macro vidoes
            [
                "data" => $vendor,
                "message" => "success",
                "status" => true,
            ],
            201
        );

    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(vendor $vendor, Request $request, $id)
    {
        try {
            $validator = Validator::make($request->all(), [
                'vname' => 'required|string|max:30',
                'address' => 'required|string|max:30',
                'contact_name' => 'required|string|max:30',
                'contact_no' => 'required|string|max:15',
                'email' => 'required|email|max:50',
                'phone' => 'nullable|string|max:15',
                'description' => 'nullable|string|max:500',
            ],[
            'vname.required' => 'Vendor name is required.',
            'vname.string' => 'Vendor name must be a string.',
            'vname.max' => 'Vendor name cannot exceed 30 characters.',
            'address.required' => 'Address is required.',
            'address.string' => 'Address must be a string.',
            'address.max' => 'Address cannot exceed 30 characters.',
            'contact_name.required' => 'Contact name is required.',
            'contact_name.string' => 'Contact name must be a string.',
            'contact_name.max' => 'Contact name cannot exceed 30 characters.',
            'contact_no.required' => 'Contact number is required.',
            'contact_no.string' => 'Contact number must be a string.',
            'contact_no.max' => 'Contact number cannot exceed 15 characters.',
            'email.required' => 'Email is required.',
            'email.email' => 'Email must be a valid email address.',
            'email.max' => 'Email cannot exceed 50 characters.',
            'phone.string' => 'Phone must be a string.',
            'phone.max' => 'Phone cannot exceed 15 characters.',
            'description.string' => 'Description must be a string.',
            'description.max' => 'Description cannot exceed 500 characters.',
        ]);

            $vendor = vendor::find($id);
            $vendor->vname = $request->vname;
            $vendor->address = $request->address;
            $vendor->contact_name = $request->contact_name;
            $vendor->contact_no = $request->contact_no;
            $vendor->email = $request->email;
            $vendor->phone = $request->phone;
            $vendor->description = $request->description;
            $vendor->save();
            return response()->json(
                [
                    "data" => $vendor,
                    "message" => ["saved"],
                    "status" => true,
                ]
            );

        } catch (IlluminateValidationValidationException $e) {
            // مدیریت خطاهای ولیدیشن
            return response()->json([
                'errors' => $e->validator->errors(),
            ], 422);
        } catch (Exception $e) {
            // مدیریت سایر خطاها
            return response()->json([
                'error' => 'Something went wrong, please try again.',
                'message' => $e->getMessage(),
            ], 500);
        }
    }

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

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