# Implementation Plan

- [ ] 1. Update database schema and TypeScript types
  - Create Supabase migration to add referred_by_id column with foreign key constraint
  - Update Contact TypeScript interface to include referred_by_id field
  - _Requirements: 3.5, 4.2_

- [ ] 2. Create AutocompleteContactInput component
  - [ ] 2.1 Implement AutocompleteContactInput component with inline contact creation
    - Create new component file at src/components/atomic-crm/contacts/AutocompleteContactInput.tsx
    - Implement contact creation handler that parses name into first_name and last_name
    - Use AutocompleteInput with onCreate prop for inline creation
    - Add proper error handling and notifications
    - _Requirements: 1.6, 1.7, 1.8_

  - [ ] 2.2 Export AutocompleteContactInput from contacts index
    - Add export to src/components/atomic-crm/contacts/index.ts
    - _Requirements: 1.6_

- [ ] 3. Update ContactInputs component
  - [ ] 3.1 Add referred_by_id field to ContactMiscInputs section
    - Import AutocompleteContactInput component
    - Add ReferenceInput for referred_by_id with filter to prevent self-referral
    - Position field between background and has_newsletter fields
    - Use proper label "Referred by"
    - _Requirements: 1.1, 1.2, 1.3, 1.4, 1.5, 3.5_

- [ ] 4. Update ContactAside component to display referrer
  - [ ] 4.1 Add referrer display section in ContactAside
    - Read ContactAside.tsx to understand current structure
    - Add conditional section to display referrer when referred_by_id exists
    - Use ReferenceField to fetch and display referrer name as clickable link
    - Match existing styling patterns in the sidebar
    - _Requirements: 2.1, 2.2, 2.3, 2.4_

- [ ] 5. Update fake data generator
  - [ ] 5.1 Modify contacts data generator to include referred_by_id
    - Update generateContacts function to initialize referred_by_id as null
    - Add logic to assign referrers to ~30% of contacts randomly
    - Ensure referrer IDs reference valid earlier contacts in the array
    - Ensure 70% of contacts have no referrer (null value)
    - _Requirements: 1.5, 2.4_

- [ ] 6. Verify implementation
  - [ ] 6.1 Test contact creation with referrer selection
    - Create a new contact and select an existing contact as referrer
    - Verify the referrer is saved correctly
    - View the contact and verify referrer appears in sidebar
    - _Requirements: 1.1, 1.2, 1.3, 1.4, 1.5, 2.1, 2.2, 2.3_

  - [ ] 6.2 Test inline contact creation from referrer field
    - Create a new contact and type a non-existent name in referrer field
    - Verify "Create contact" option appears
    - Create the new contact inline
    - Verify the newly created contact is selected as referrer
    - _Requirements: 1.6, 1.7, 1.8_

  - [ ] 6.3 Test contact editing and referrer updates
    - Edit an existing contact and change the referrer
    - Edit a contact and clear the referrer field
    - Verify self-referral is prevented
    - _Requirements: 3.1, 3.2, 3.3, 3.4, 3.5_

  - [ ] 6.4 Test referrer deletion behavior
    - Create a contact with a referrer
    - Delete the referrer contact
    - Verify the referred contact's referrer field is cleared (null)
    - Verify no errors occur when viewing the contact
    - _Requirements: 4.1, 4.2, 4.3, 4.4_

  - [ ] 6.5 Test with fake data
    - Switch to fakerest provider
    - Verify contacts are generated with varied referrer data
    - Verify ~30% have referrers and ~70% have no referrer
    - Verify referrer links work correctly in the UI
    - _Requirements: 1.5, 2.1, 2.2, 2.3, 2.4_
