# Child Company API - cURL Examples

## Base URL
```
http://localhost:3001
```

## Authentication
Both endpoints require admin authentication. You need to login first and get an `auth_token`, then include it in the `Authorization` header.

### Step 1: Admin Login (Get Auth Token)
```bash
curl -X POST http://localhost:3001/admin/login \
  -H "Content-Type: application/json" \
  -d '{
    "username": "super_admin",
    "password": "admin123",
    "company_id": 1
  }'
```

**Response:**
```json
{
  "status": "success",
  "message": "Login successful",
  "auth_token": "your_auth_token_here",
  "admin": {
    "id": 1,
    "name": "Super Admin",
    "username": "super_admin",
    "company_id": 1
  }
}
```

---

## 1. Create Child Company

**Endpoint:** `POST /admin/create_child_company`

**Required Fields:**
- `com_name` - Company name
- `mob_num` - Mobile number
- `email` - Email address
- `user_name` - Username for the child company
- `password` - Password for the child company

**Optional Fields:**
- `off_num` - Office number
- `street` - Street address
- `city` - City
- `district` - District
- `pincode` - Pincode
- `gst_no` - GST number
- `inherit_settings` - Inherit settings from parent (default: true)
- `inherit_loan_types` - Inherit loan types from parent (default: true)
- `inherit_ledger_structure` - Inherit ledger structure from parent (default: true)

### cURL Example (Minimal - Required Fields Only)
```bash
curl -X POST http://localhost:3001/admin/create_child_company \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_AUTH_TOKEN_HERE" \
  -d '{
    "com_name": "Child Company Name",
    "mob_num": "9876543210",
    "email": "child@example.com",
    "user_name": "child_admin",
    "password": "child123"
  }'
```

### cURL Example (Complete - All Fields)
```bash
curl -X POST http://localhost:3001/admin/create_child_company \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer YOUR_AUTH_TOKEN_HERE" \
  -d '{
    "com_name": "Child Company Name",
    "mob_num": "9876543210",
    "off_num": "080-12345678",
    "email": "child@example.com",
    "street": "123 Main Street",
    "city": "Bangalore",
    "district": "Bangalore Urban",
    "pincode": "560001",
    "gst_no": "29ABCDE1234F1Z5",
    "user_name": "child_admin",
    "password": "child123",
    "inherit_settings": true,
    "inherit_loan_types": true,
    "inherit_ledger_structure": true
  }'
```

### Success Response (201 Created)
```json
{
  "status": "success",
  "message": "Child company created successfully",
  "data": {
    "registration": {
      "id": 7,
      "com_name": "Child Company Name",
      "email": "child@example.com",
      "status": "active"
    },
    "company": {
      "com_id": 16,
      "reg_id": 7,
      "com_name": "Child Company Name",
      "user_name": "child_admin",
      "email": "child@example.com",
      "parent_com_id": 1,
      "company_type": "child"
    },
    "parent": {
      "com_id": 1,
      "com_name": "Parent Company Name"
    },
    "inherited": {
      "ledger_structure": true,
      "loan_types": true
    }
  }
}
```

### Error Responses

**400 Bad Request - Missing Required Fields:**
```json
{
  "status": "error",
  "message": "Required fields missing: com_name, mob_num, email, user_name, password"
}
```

**400 Bad Request - Username/Email Already Exists:**
```json
{
  "status": "error",
  "message": "Username already exists"
}
```

**401 Unauthorized - No Auth Token:**
```json
{
  "status": "error",
  "message": "Authentication token required"
}
```

**401 Unauthorized - No Company ID:**
```json
{
  "status": "error",
  "message": "Parent company ID not found. Please ensure you are authenticated as an admin with a company."
}
```

**404 Not Found - Parent Company Not Found:**
```json
{
  "status": "error",
  "message": "Parent company not found"
}
```

---

## 2. Get Child Companies

**Endpoint:** `GET /admin/child_companies`

**No Request Body Required**

### cURL Example
```bash
curl -X GET http://localhost:3001/admin/child_companies \
  -H "Authorization: Bearer YOUR_AUTH_TOKEN_HERE"
```

### Alternative (Using Query Parameter)
```bash
curl -X GET "http://localhost:3001/admin/child_companies?auth_token=YOUR_AUTH_TOKEN_HERE"
```

### Success Response (200 OK)
```json
{
  "status": "success",
  "message": "Child companies retrieved successfully",
  "data": {
    "parent_company_id": 1,
    "total_children": 2,
    "children": [
      {
        "company_id": 16,
        "registration_id": 7,
        "company_name": "Child Company 1",
        "mobile": "9876543210",
        "email": "child1@example.com",
        "location": "Bangalore, Bangalore Urban",
        "username": "child_admin_1",
        "registration_status": "active",
        "created_at": "2024-12-04T10:30:00.000Z"
      },
      {
        "company_id": 17,
        "registration_id": 8,
        "company_name": "Child Company 2",
        "mobile": "9876543211",
        "email": "child2@example.com",
        "location": "Mumbai, Mumbai Suburban",
        "username": "child_admin_2",
        "registration_status": "active",
        "created_at": "2024-12-04T11:00:00.000Z"
      }
    ]
  }
}
```

### Success Response (No Children)
```json
{
  "status": "success",
  "message": "Child companies retrieved successfully",
  "data": {
    "parent_company_id": 1,
    "total_children": 0,
    "children": []
  }
}
```

### Error Responses

**401 Unauthorized - No Auth Token:**
```json
{
  "status": "error",
  "message": "Authentication token required"
}
```

**401 Unauthorized - No Company ID:**
```json
{
  "status": "error",
  "message": "Parent company ID not found. Please ensure you are authenticated as an admin with a company."
}
```

**500 Internal Server Error:**
```json
{
  "status": "error",
  "message": "Failed to fetch child companies",
  "error": "Error message details"
}
```

---

## Complete Workflow Example

### Step 1: Login and Get Token
```bash
# Login
TOKEN=$(curl -s -X POST http://localhost:3001/admin/login \
  -H "Content-Type: application/json" \
  -d '{
    "username": "super_admin",
    "password": "admin123",
    "company_id": 1
  }' | jq -r '.auth_token')

echo "Auth Token: $TOKEN"
```

### Step 2: Create Child Company
```bash
curl -X POST http://localhost:3001/admin/create_child_company \
  -H "Content-Type: application/json" \
  -H "Authorization: Bearer $TOKEN" \
  -d '{
    "com_name": "New Child Company",
    "mob_num": "9876543210",
    "email": "newchild@example.com",
    "user_name": "newchild_admin",
    "password": "newchild123",
    "city": "Bangalore",
    "district": "Bangalore Urban"
  }'
```

### Step 3: Get All Child Companies
```bash
curl -X GET http://localhost:3001/admin/child_companies \
  -H "Authorization: Bearer $TOKEN"
```

---

## Notes

1. **Authentication**: Always include the `auth_token` in the `Authorization` header as `Bearer TOKEN` or as a query parameter `?auth_token=TOKEN`

2. **Transaction Safety**: The create endpoint uses database transactions, so if any step fails, all changes are rolled back

3. **Inheritance**: By default, child companies inherit:
   - Ledger structure (LedgerUnder entries)
   - Loan types
   - A default "Cash A/c" ledger entry is created

4. **Registration Mapping**: Each child company is automatically linked to a registration entry via `reg_id = registration.id`

5. **Staff Creation**: An admin staff record is automatically created for the child company with the provided username and password

6. **Uniqueness**: Username and email must be unique across all companies

