To solve this optimization problem, we first need to define the variables and the objective function. Let's denote:

- \(x_A\) as the amount invested in toothpaste company A.
- \(x_B\) as the amount invested in toothpaste company B.

The objective is to maximize profit, given by \(0.12x_A + 0.14x_B\).

We have the following constraints:
1. The total investment cannot exceed $5000: \(x_A + x_B \leq 5000\).
2. At least 40% of the total investment must be in company A: \(x_A \geq 0.4(x_A + x_B)\).
3. At least $1000 must be invested in company B: \(x_B \geq 1000\).

Now, let's convert these constraints and the objective function into Gurobi code to find the optimal investment amounts.

```python
from gurobipy import *

# Create a new model
m = Model("Toothpaste_Investment")

# Define variables
x_A = m.addVar(lb=0, name="Investment_in_A")
x_B = m.addVar(lb=0, name="Investment_in_B")

# Set the objective function: Maximize profit
m.setObjective(0.12*x_A + 0.14*x_B, GRB.MAXIMIZE)

# Add constraints
m.addConstr(x_A + x_B <= 5000, "Total_Investment")
m.addConstr(x_A >= 0.4*(x_A + x_B), "Minimum_Investment_in_A")
m.addConstr(x_B >= 1000, "Minimum_Investment_in_B")

# Optimize the model
m.optimize()

# Print the results
if m.status == GRB.OPTIMAL:
    print("Optimal investment in Company A: $", round(x_A.x, 2))
    print("Optimal investment in Company B: $", round(x_B.x, 2))
    print("Maximum profit: $", round(m.objVal, 2))
else:
    print("Model is not optimal. Status:", m.status)

```