# Contacts Management

## Overview

The Contacts Management system allows you to manage contacts that can be related to clients. Each client can have multiple contacts, and contacts can also exist independently without being assigned to a client.

## Features

### Contact Information
- **Name**: Required field for contact identification
- **Mobile Phone**: Contact's mobile phone number
- **Company**: Company the contact works for
- **Country**: Contact's country of residence
- **Position**: Job title or position at the company
- **LinkedIn Profile**: URL to the contact's LinkedIn profile
- **Email**: Contact's email address
- **Notes**: Additional notes about the contact
- **Client Assignment**: Optional assignment to a specific client

### LinkedIn Integration
- **URL Validation**: Validates LinkedIn profile URLs
- **Data Fetching**: Placeholder for future LinkedIn API integration
- **Auto-fill**: Automatically populates contact information from LinkedIn (when API is available)

### Management Features
- **Search**: Search contacts by name, company, email, or phone
- **Filtering**: Filter by company, country, or assigned client
- **Pagination**: Paginated results for better performance
- **CRUD Operations**: Create, read, update, and delete contacts
- **Client Relationships**: Associate contacts with clients

## Database Schema

### Contacts Table
```sql
CREATE TABLE contacts (
    id BIGINT PRIMARY KEY,
    uuid VARCHAR(255) UNIQUE,
    name VARCHAR(255) NOT NULL,
    mobile_phone VARCHAR(255) NULL,
    company VARCHAR(255) NULL,
    country VARCHAR(255) NULL,
    position VARCHAR(255) NULL,
    linkedin_profile VARCHAR(255) NULL,
    email VARCHAR(255) NULL,
    notes TEXT NULL,
    client_id BIGINT NULL,
    created_at TIMESTAMP NULL,
    updated_at TIMESTAMP NULL,
    FOREIGN KEY (client_id) REFERENCES clients(id) ON DELETE SET NULL
);
```

## Models

### Contact Model
- Uses `HasUuid` trait for UUID generation
- Belongs to `Client` model
- Has morphMany relationship with `Interaction` model
- Includes helper methods for LinkedIn URL formatting and contact info display

### Client Model
- Has many `Contact` models
- Can have multiple contacts assigned

## Livewire Components

### ContactsManager
- Main component for listing and managing contacts
- Includes search and filtering functionality
- Handles contact deletion with confirmation

### CreateContact
- Form component for creating new contacts
- Includes LinkedIn integration section
- Validates required fields
- Auto-fills form from LinkedIn data (when available)

### EditContact
- Form component for editing existing contacts
- Pre-populates form with current contact data
- Includes LinkedIn integration section

## Routes

```php
// Contacts Management
Route::get('/contacts', ContactsManager::class)->name('contacts.index');
Route::get('/contacts/create', CreateContact::class)->name('contacts.create');
Route::get('/contacts/{contact}/edit', EditContact::class)->name('contacts.edit');
```

## Services

### LinkedinService
- Validates LinkedIn URLs
- Extracts usernames from LinkedIn URLs
- Placeholder for future LinkedIn API integration
- Handles different LinkedIn URL formats

## Usage

### Creating a Contact
1. Navigate to `/contacts/create`
2. Fill in the contact information
3. Optionally enter a LinkedIn profile URL and click "Fetch Data"
4. Assign to a client (optional)
5. Save the contact

### Managing Contacts
1. Navigate to `/contacts`
2. Use search and filters to find specific contacts
3. Click "Edit" to modify a contact
4. Click "Delete" to remove a contact

### Linking to Clients
- Contacts can be assigned to clients during creation or editing
- Unassigned contacts can be managed independently
- Client assignment is optional

## Future Enhancements

### LinkedIn API Integration
- Implement actual LinkedIn API integration
- Fetch real contact data from LinkedIn profiles
- Handle API rate limits and authentication

### Advanced Features
- Contact import/export functionality
- Bulk operations (delete, assign to client)
- Contact activity tracking
- Email integration
- Contact categorization and tagging

## Testing

Run the contact tests with:
```bash
php artisan test tests/Feature/ContactTest.php
```

## Seeding

Seed sample contacts with:
```bash
php artisan db:seed --class=ContactSeeder
```

## Permissions

The system includes permission checks for contact deletion:
- `contacts.delete` permission required for deletion operations
- Other operations are available to all authenticated users 

## LinkedIn Integration

### How It Works
- **URL Validation**: Validates LinkedIn profile URLs
- **Data Extraction**: Extracts username from LinkedIn URLs
- **Mock Data Generation**: Generates realistic contact data based on the username
- **Auto-fill**: Automatically populates contact information from LinkedIn

### Supported URL Formats
- `https://www.linkedin.com/in/username`
- `https://linkedin.com/in/username`
- `https://www.linkedin.com/pub/username`
- `https://linkedin.com/pub/username`

### Usage
1. Enter a LinkedIn profile URL in the "LinkedIn Profile URL" field
2. Click "Fetch Data" button
3. The form will be automatically populated with:
   - **Name**: Generated from the username
   - **Company**: Random selection from predefined companies
   - **Position**: Random selection from predefined positions
   - **Email**: Generated based on name and company domain
   - **Phone**: Generated random phone number

### Example
**Input URL**: `https://www.linkedin.com/in/john-doe`
**Generated Data**:
- Name: John Doe
- Company: TechCorp Solutions
- Position: Senior Developer
- Email: john.doe@techcorp.com
- Phone: +1-555-0123

### Future Enhancements
- Real LinkedIn API integration
- Web scraping of public LinkedIn profiles
- More accurate data extraction
- Company and position matching 