#!/bin/bash
# Script to update ProfilePage.php with IC number validation

FILE="app/Livewire/ProfilePage.php"

# Backup
cp "$FILE" "${FILE}.bak"

# 1. Add import after line 14 (after "use Livewire\WithFileUploads;")
sed -i '' '14 a\
use App\\Rules\\MalaysianIcNumber;
' "$FILE"

# 2. Update saveProfile ic_number (around line 248)
sed -i '' "s/'ic_number' => \$this->ic_number,/'ic_number' => MalaysianIcNumber::clean(\$this->ic_number),/" "$FILE"

# 3. Update saveProfile husband_ic_number (around line 264)
sed -i '' "s/'husband_ic_number' => \$this->husband_ic_number,/'husband_ic_number' => MalaysianIcNumber::clean(\$this->husband_ic_number),/" "$FILE"

# 4. Update saveMotherInfo validation (around line 335)
sed -i '' "s/'ic_number' => 'required|string',/'ic_number' => ['required', 'string', new MalaysianIcNumber],/" "$FILE"

# 5. Update saveMotherInfo save ic_number (around line 351)  
perl -i -pe "s/(\s+)'ic_number' => \\\$this->ic_number,/\$1'ic_number' => MalaysianIcNumber::clean(\\\$this->ic_number),/" "$FILE"

# 6. Update saveHusbandInfo validation (around line 389)
sed -i '' "s/'husband_ic_number' => 'nullable|string',/'husband_ic_number' => ['nullable', 'string', new MalaysianIcNumber],/" "$FILE"

# 7. Update saveHusbandInfo save husband_ic_number (around line 398)
perl -i -pe "s/(\s+)'husband_ic_number' => \\\$this->husband_ic_number,/\$1'husband_ic_number' => MalaysianIcNumber::clean(\\\$this->husband_ic_number),/" "$FILE"

echo "ProfilePage.php updated successfully"
php artisan route:clear > /dev/null 2>&1
echo "Route cache cleared"
