import re

path = 'resources/views/admin/employees/index.blade.php'
with open(path, 'r') as f:
    content = f.read()

content = content.replace('governingBodyManager', 'employeeManager')
content = content.replace('Governing Bodies', 'Employees')
content = content.replace('Add Member', 'Add Employee')
content = content.replace('governingBodyModal', 'employeeModal')
content = content.replace('governingBodyForm', 'employeeForm')
content = content.replace('governing-bodies', 'employees')

# Remove summary and message fields in the HTML
content = re.sub(r'<!-- Summary -->.*?</div>\s*<!-- Message -->.*?</div>\s*<!-- Bio PDF -->', '<!-- Bio PDF -->', content, flags=re.DOTALL)

# Replace Type filter with Department filter
dept_filter = '''<div class="form-group">
                <label class="form-label">Department</label>
                <select class="form-select" x-model="filters.department_id">
                    <option value="">All Departments</option>
                    <template x-for="dept in departments" :key="dept.id">
                        <option :value="dept.id" x-text="dept.name"></option>
                    </template>
                </select>
            </div>'''
content = re.sub(r'<div class="form-group">\s*<label class="form-label">Type</label>.*?</div>', dept_filter, content, flags=re.DOTALL)

# Replace Type column header
content = content.replace('<th>Type</th>', '<th>Department</th>')

# Replace Type column data
dept_col = '''<td><span class="badge bg-info text-dark" x-text="item.department_name ?? \'\'"></span></td>'''
content = re.sub(r'<td>\s*<span class="badge bg-info text-dark"\s*x-text="\s*item\.type ===.*?</span>\s*</td>', dept_col, content, flags=re.DOTALL)

# Replace Type input in Add Modal
dept_input = '''<div class="col-md-6 mb-3">
                                    <label class="form-label">Department <span class="text-danger">*</span></label>
                                    <select class="form-select" x-model="form.department_id">
                                        <option value="">Select Department</option>
                                        <template x-for="dept in departments" :key="dept.id">
                                            <option :value="dept.id" x-text="dept.name"></option>
                                        </template>
                                    </select>
                                    <div class="text-danger small mt-1" x-text="errors.department_id"></div>
                                </div>'''
content = re.sub(r'<!-- Type -->.*?</div>\s*<!-- Status -->', '<!-- Department -->\n' + dept_input + '\n<!-- Status -->', content, flags=re.DOTALL)

# In x-data function, add departments: []
content = content.replace('items: [],', 'items: [], departments: [],')

# Update filters
content = content.replace('type: \'\',', 'department_id: \'\',')

# Update form init
content = content.replace('summary: \'\',', '')
content = content.replace('message: \'\',', '')
content = content.replace('type: \'executive_committe\',', 'department_id: \'\',')

# In fetchItems add department
content = content.replace('if (this.filters.type) params.append(\'type\', this.filters.type);', 'if (this.filters.department_id) params.append(\'department_id\', this.filters.department_id);')

# Fetch departments
fetch_depts = '''async fetchDepartments() {
                    try {
                        const res = await fetch(`{{ route('admin.employees.departments') }}`);
                        const json = await res.json();
                        this.departments = json.data;
                    } catch (e) {
                        console.error('Failed to load departments.');
                    }
                },'''

content = content.replace('async fetchItems', fetch_depts + '\n                async fetchItems')

# Add fetchDepartments to x-init
content = content.replace('x-init="fetchItems()"', 'x-init="fetchDepartments(); fetchItems();"')

# Update edit form load
content = content.replace('this.form.summary = s.summary ?? \'\';', '')
content = content.replace('this.form.message = s.message ?? \'\';', '')
content = content.replace('this.form.type = s.type;', 'this.form.department_id = s.department_id;')

# Update formData append
content = content.replace('formData.append(\'summary\', this.form.summary ?? \'\');', '')
content = content.replace('formData.append(\'message\', this.form.message ?? \'\');', '')
content = content.replace('formData.append(\'type\', this.form.type);', 'formData.append(\'department_id\', this.form.department_id);')

# Write back
with open(path, 'w') as f:
    f.write(content)
