<?php

namespace App\Http\Controllers;

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

class ExportShipToAddressController extends Controller
{
    public function index()
    {
        $addresses = ExportShipToAddress::all();
        $countries = config('constants.countries');
        return view('export.ship_to_address.index', compact('addresses', 'countries'));
    }

    public function store(Request $request)
    {
        $request->validate([
            'contact_name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'company_name' => 'nullable|string|max:255',
            'address_1' => 'required|string|max:255',
            'address_2' => 'nullable|string|max:255',
            'address_3' => 'nullable|string|max:255',
            'city' => 'required|string|max:255',
            'zip_code' => 'required|string|max:255',
            'country' => 'required|string|max:255',
            'phone_country_code' => 'required|string|max:255',
            'phone_number' => 'required|string|max:255',
        ]);

        ExportShipToAddress::create($request->all());

        return redirect()->route('export.ship_to_address.index')
            ->with('success', 'Address created successfully.');
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'contact_name' => 'required|string|max:255',
            'email' => 'required|email|max:255',
            'company_name' => 'nullable|string|max:255',
            'address_1' => 'required|string|max:255',
            'address_2' => 'nullable|string|max:255',
            'address_3' => 'nullable|string|max:255',
            'city' => 'required|string|max:255',
            'zip_code' => 'required|string|max:255',
            'country' => 'required|string|max:255',
            'phone_country_code' => 'required|string|max:255',
            'phone_number' => 'required|string|max:255',
        ]);

        $address = ExportShipToAddress::findOrFail($id);
        $address->update($request->all());

        return redirect()->route('export.ship_to_address.index')
            ->with('success', 'Address updated successfully.');
    }

    public function show($id)
    {
        $address = ExportShipToAddress::findOrFail($id);
        return view('export.ship_to_address.show', compact('address'));
    }

    public function destroy($id)
    {
        $address = ExportShipToAddress::findOrFail($id);
        $address->delete();

        return redirect()->route('export.ship_to_address.index')
            ->with('success', 'Address deleted successfully.');
    }
}
