# Doctor Configuration Module - Usage Guide

## Overview
The Doctor Configuration module allows you to create dynamic consultation forms based on doctor specializations. The system supports:
- Categories of questions (Chief Complaint, Medical History, etc.)
- Different field types (text, textarea, select, checkbox, radio)
- Specialist-specific questions
- Dynamic form generation

## Basic Usage

### 1. Access the Configuration
Navigate to **Doctor Configuration** in the sidebar (requires admin permissions).

### 2. Managing Categories
- View all categories with their items count
- Create new categories with icons and descriptions
- Edit existing categories
- Toggle category status (active/inactive)
- Delete categories (only if no items exist)

### 3. Managing Items
- Click on a category to view/manage its items
- Create new questions with different field types
- Set specialist-specific questions
- Configure options for select/checkbox/radio fields

## Service Usage

### Loading Questions for a Doctor

```php
use App\Services\DoctorConfigurationService;

$service = new DoctorConfigurationService();

// Get all questions for a general doctor
$questions = $service->getQuestionsForDoctor();

// Get questions for a specific specialist
$cardiologyQuestions = $service->getQuestionsForDoctor('Cardiologist');
```

### Generating a Consultation Form

```php
$service = new DoctorConfigurationService();

// Generate form structure for a specific specialist
$form = $service->generateConsultationForm('Pediatrician');

// Example output:
[
    [
        'id' => 1,
        'name' => 'Chief Complaint',
        'icon' => 'fa fa-stethoscope',
        'description' => 'Main reason for the visit',
        'fields' => [
            [
                'id' => 1,
                'label' => 'Presenting Complaint',
                'question' => 'What is the main reason for your visit today?',
                'type' => 'textarea',
                'required' => true,
                'options' => null,
                'validation' => 'string'
            ]
        ]
    ]
]
```

### Using with User Model

```php
// Assuming $user is a User instance with specialist_type set
$user = Auth::user();

// Get questions for this specific doctor
$questions = $user->getDoctorConfigurationQuestions();

// Generate consultation form
$form = $user->generateConsultationForm();

// Validate answers
$answers = [
    1 => 'Patient has chest pain',
    2 => 'Since yesterday',
    // ... more answers
];

$validation = $user->validateConsultationAnswers($answers);
if ($validation['valid']) {
    // Process the answers
    $cleanData = $validation['data'];
} else {
    // Handle validation errors
    $errors = $validation['errors'];
}
```

### Validating and Formatting Data

```php
$service = new DoctorConfigurationService();

// Validate consultation answers
$answers = [
    1 => 'Chest pain and shortness of breath',
    2 => 'Since this morning',
    3 => 'Severe',
    4 => ['Yes']
];

$validation = $service->validateConsultationAnswers($answers, 'Cardiologist');

if ($validation['valid']) {
    // Format data for storage or display
    $formattedData = $service->formatConsultationData($answers, 'Cardiologist');

    // Store in database or process further
    // $formattedData contains structured consultation data
}
```

## Field Types

### Text
- Single line input
- Validation: max 255 characters

### Textarea
- Multi-line input
- No character limit

### Select
- Dropdown selection
- Single value from predefined options

### Checkbox
- Multiple selections
- Array of values from predefined options

### Radio
- Single selection
- One value from predefined options

## Specialist Configuration

### Setting Up Specialist Questions
1. Create a category or item
2. Add specialist types when creating items
3. Questions will only appear for doctors with matching specialist types

### Available Specialist Types (from seeded data)
- Cardiologist
- Pulmonologist
- Neurologist
- Pediatrician
- Geneticist

## Example Implementation in Consultation

```php
// In your consultation controller/livewire component

class ConsultationComponent extends Component
{
    public $consultationAnswers = [];
    public $doctorForm = [];

    public function mount($patientId)
    {
        $this->doctorForm = Auth::user()->generateConsultationForm();
    }

    public function saveConsultation()
    {
        $validation = Auth::user()->validateConsultationAnswers($this->consultationAnswers);

        if (!$validation['valid']) {
            foreach ($validation['errors'] as $field => $error) {
                $this->addError("consultationAnswers.{$field}", $error);
            }
            return;
        }

        $service = new DoctorConfigurationService();
        $formattedData = $service->formatConsultationData(
            $this->consultationAnswers,
            Auth::user()->specialist_type
        );

        // Store consultation data
        Consultation::create([
            'patient_id' => $this->patientId,
            'doctor_id' => Auth::id(),
            'consultation_data' => json_encode($formattedData),
            // ... other fields
        ]);

        toastSuccess('Consultation saved successfully!');
    }
}
```

## Database Structure

### Tables Created:
- `doctor_config_categories`: Question categories
- `doctor_config_items`: Individual questions/fields
- `doctor_specialist_items`: Maps items to specialist types

### Relationships:
- Category → hasMany Items
- Item → belongsTo Category
- Item → hasMany SpecialistItems
- SpecialistItem → belongsTo Item

## Seeded Data
The system comes with pre-configured categories:
- Chief Complaint
- Medical History
- Review of Systems
- Social History
- Family History
- Pediatric Assessment

Each category contains relevant questions with appropriate field types and specialist mappings.