# TODO: Allow Users to Join Cooperatives via User Settings

## Overview
Implement functionality to allow users to join cooperatives stored in the user's `settings` JSON field, with a dedicated composable and controller for managing user additional details and enabling quick search of users by cooperative membership.

---

## Step 1: Database Schema Update
- [ ] Verify that `settings` field in `users` table is already JSON type (already exists and cast as array)
- [ ] No migration needed - `settings` field already supports array data

---

## Step 2: Create UserAdditionalDetailsController
- [ ] Create `app/Http/Controllers/UserManagement/UserAdditionalDetailsController.php`
- [ ] Implement `getDetails()` method - Retrieve user's additional details including cooperatives
- [ ] Implement `updateCooperatives()` method - Add/remove cooperatives from user settings
- [ ] Implement `searchUsersByCooperative()` method - Quick search for users by cooperative hashkey
- [ ] Implement `getUserCooperatives()` method - Get all cooperatives a user has joined
- [ ] Use ResponseHelper for consistent API responses
- [ ] Add proper authorization checks using UserPermissions

---

## Step 3: Create useUserAdditionalDetails Composable
- [ ] Create `resources/js/composables/useUserAdditionalDetails.js`
- [ ] Implement `fetchUserDetails()` - Fetch current user's additional details
- [ ] Implement `joinCooperative(cooperativeHash)` - Add cooperative to user's settings
- [ ] Implement `leaveCooperative(cooperativeHash)` - Remove cooperative from user's settings
- [ ] Implement `getJoinedCooperatives()` - Get list of cooperatives user has joined
- [ ] Implement `searchUsersByCooperative(cooperativeHash)` - Search users by cooperative
- [ ] Add loading states and error handling
- [ ] Follow existing composable patterns (useUserSettings.js as reference)

---

## Step 4: Register Routes
- [ ] Add routes in `routes/api.php` or appropriate route file:
  - `POST /UserAdditionalDetails/Get` - Get user details
  - `POST /UserAdditionalDetails/UpdateCooperatives` - Update cooperatives in settings
  - `POST /UserAdditionalDetails/SearchByCooperative` - Search users by cooperative
  - `POST /UserAdditionalDetails/GetCooperatives` - Get user's cooperatives

---

## Step 5: Update User Model (if needed)
- [ ] Verify `settings` field is in `$fillable` array (already present)
- [ ] Verify `settings` field is cast as `array` (already present)
- [ ] Add helper method `getCooperativesAttribute()` to easily access cooperatives from settings
- [ ] Add helper method `hasJoinedCooperative($cooperativeHash)` to check membership

---

## Step 6: Integration with Existing Cooperative System
- [ ] Ensure compatibility with existing `CooperativeController::joinCooperative()`
- [ ] When user joins via CooperativeController, also update user settings
- [ ] Maintain consistency between `cooperative_members` table and user settings

---

## Step 7: Testing & Validation
- [ ] Test joining a cooperative updates user settings correctly
- [ ] Test leaving a cooperative removes from user settings
- [ ] Test quick search returns correct users by cooperative
- [ ] Test authorization and permission checks
- [ ] Test edge cases (duplicate joins, non-existent cooperatives)

---

## Files to Create/Modify
1. **NEW**: `app/Http/Controllers/UserManagement/UserAdditionalDetailsController.php`
2. **NEW**: `resources/js/composables/useUserAdditionalDetails.js`
3. **MODIFY**: `routes/api.php` (or appropriate route file)
4. **MODIFY**: `app/Models/User.php` (add helper methods)
5. **MODIFY**: `app/Http/Controllers/Market/CooperativeController.php` (sync with settings)

---

## Technical Notes
- Cooperatives will be stored in `settings.cooperatives` as an array of hashkeys
- Example structure: `settings: { cooperatives: ['hash1', 'hash2', ...] }`
- Quick search should use JSON query to find users with specific cooperative in settings
- Follow existing patterns from `useUserSettings.js` and `CooperativeController.php`