import re

file_path = 'index.php'

with open(file_path, 'r') as f:
    content = f.read()

# 1. Replace <div class="row g-3"> with <div class="form-grid-3">
# We want to be careful. The user specifically mentioned "Items edit form" and "Add new items form".
# And "Apply the style to all forms in app".
# "row g-3" is the standard Bootstrap form layout class. Replacing it with "form-grid-3" (which I defined)
# should be safe for forms.
# However, "row g-3" might be used for non-form layouts (e.g. stats cards).
# I should look for inputs inside.
# But checking content inside is hard with regex.

# Let's count how many "row g-3" there are.
matches = len(re.findall(r'class=["\']row g-3["\']', content))
print(f"Found {matches} instances of 'row g-3'.")

# Strategy:
# Replace 'class="row g-3"' with 'class="form-grid-3"' IF it's inside a form or modal context.
# Or just replace all? "row g-3" implies a grid with gap 3 (1rem).
# My "form-grid-3" is a grid with gap 1rem and 3 columns.
# If "row g-3" was used for 2 columns (col-6) or 4 columns (col-3), forcing 3 columns might break layout.
# BUT the user said "Apply the style to all forms".
# Most "row g-3" in this app seem to be forms (based on previous grep).
# Non-form grids usually use "row" without "g-3" or with different gap?
# Let's assume "row g-3" is the form standard here.

# I'll define a function to replace specific occurrences if I can identify them.
# The "Edit Item" modal is around line 4964.
# The "Add Item" modal is around line 10141.

# Regex to find <div class="row g-3"> and replace it, but maybe verify context?
# I'll just do a global replace for now, but I will backup first.
# Wait, "row g-3" in a dashboard stats widget (e.g. 4 cards) would become 3 columns.
# That might be annoying.
# I should inspect if there are stats widgets using "row g-3".

# Let's look for "col-md-3" or "col-xl-3" inside "row g-3".
# If I see "col-xl-3", it's likely a 4-column layout (12/3 = 4).
# My "form-grid-3" forces 3 columns.
# So replacing it would break 4-column layouts.
# "Edit Item" uses "col-md-3", "col-md-4", "col-md-6".
# If I change the container to grid-3, the children become grid items.
# I added CSS to make children width 100%.

# To be safer, I will only replace "row g-3" if it contains "form-control" or "form-select" or "form-label" inside it (heuristic).
# This is tricky with regex.

# Alternative: Find the specific blocks for Items and change them.
# Then find other *forms*.

# Let's use a simpler approach.
# I will search for the specific lines for Edit Item and Add Item and change them.
# Then I will search for other obvious forms.

new_content = content

# 1. Edit Item Modal (around line 4975)
# Context: <div class="modal fade" id="editItemModal...
# ... <form ...> ... <div class="modal-body"> ... <div class="row g-3">
# I'll use regex to match the structure.

# Pattern for Edit/Add Item Modals (and potentially others)
# Matches <div class="row g-3"> inside a form or modal body?
# Actually, the user wants "items edit form" specifically.

# I'll replace specifically in the Item Modals first.
# "Edit Item" has id="editItemModal..."
# "Add Item" has id="addItemModal"

# But "Edit Item" is inside a PHP loop? No, the modal ID has PHP echo.
# L4965: <div class="modal fade" id="editItemModal<?= $item['id'] ?>" ...
# ...
# L4975: <div class="row g-3">

# I'll verify the lines again.
# L4975 is likely correct.

# I'll proceed with a more manual replacement for the Items forms to ensure they are fixed.
# Then I'll check "Edit Profile" (L8558) and "Company Profile" (L8594).

def replace_block(text, start_marker, end_marker, old_class, new_class):
    # Find start marker
    pattern = re.compile(re.escape(start_marker) + r'.*?' + re.escape(old_class), re.DOTALL)
    # This is hard because of multiple occurrences.
    pass

# Direct replacement on known lines (approximate) is safer if I can locate them uniquely.
# Or replace all "row g-3" that are inside <form>?
# That covers "all forms".

# Let's try to replace <div class="row g-3"> with <div class="form-grid-3"> everywhere,
# BUT checking if it looks like a form.
# A form usually has <input>, <select>, <label>.

# I'll iterate through all "row g-3" occurrences.
# For each, I'll check if the following content (up to closing div) contains form elements.
# This requires parsing HTML, which is hard.

# Let's go with the user's specific request + "all forms" interpretation.
# I will replace `class="row g-3"` with `class="form-grid-3"` ONLY if the immediate context looks like a form.
# I.e. if I see `<form ... class="row g-3">` -> Replace.
# If I see `<div class="row g-3">` followed closely by `<div class="col`.

# Let's use a regex that matches `<div class="row g-3">` followed by whitespace and `<div class="col`.
# Most forms follow this pattern.

# Pattern: <div class="row g-3">\s*<div class="col
regex_div = r'(<div class="row g-3">)(\s*<div class="col)'
new_content = re.sub(regex_div, r'<div class="form-grid-3">\2', new_content)

# Pattern: <form ... class="row g-3">
regex_form = r'(<form [^>]*class=")(row g-3)(")'
new_content = re.sub(regex_form, r'\1form-grid-3\3', new_content)

# Also explicitly fix the Items modals if they weren't caught (e.g. if there's comment or something in between).
# L4975: <div class="row g-3"> (inside Edit Item).
# It has <div class="col-md-6"> immediately after?
# Let's check L4975 in `read_file` output.
# L4975: <div class="row g-3">
# L4976:     <div class="col-md-6">
# Yes, it matches.

# L10151 (Add Item):
# L10151: <div class="row g-3">
# L10152:     <div class="col-md-6">
# Matches.

# So the regex `(<div class="row g-3">)(\s*<div class="col)` should catch them.

# One more thing: The user asked to "Add background to titles bar".
# I've added CSS for `.modal-header`.
# But for non-modals (like Edit Profile), they use `<h5>`.
# I should try to wrap them or add a class.
# L8554: <h5 class="mb-4 fw-bold" data-en="Edit Profile"...
# I'll use regex to find these specific h5 titles in cards and wrap them or add a class.
# Search for `class="card p-4 ...">` then `<h5>`.
# This is too brittle. I'll stick to the CSS solution for Modals and hope it's enough for "titles bar".
# The user specifically mentioned "Items edit form" (which is a modal).
# If "Edit Profile" doesn't have a background title, I can manually fix it if requested or if I can find it reliably.
# I'll stick to the safe replacement for now.

with open(file_path, 'w') as f:
    f.write(new_content)

print("Replacement complete.")
