{
  "info": {
    "name": "CallCRM Voice OTP",
    "description": "Voice OTP test flow. Run in order: 1 Login → 2 Send → 3 Status → 4 Verify (wrong) → 5 Verify (correct). Set baseUrl, email, password and phone first. Login saves the token; Send saves otp_id and code automatically.\n\nNOTE: the real phone call is not built yet — Send creates and queues the OTP but no call reaches the phone. Send/Status/Verify, rate limits and the 5-try lock can be tested now.",
    "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json"
  },
  "auth": {
    "type": "bearer",
    "bearer": [{ "key": "token", "value": "{{token}}", "type": "string" }]
  },
  "variable": [
    { "key": "baseUrl", "value": "http://127.0.0.1:8000/api" },
    { "key": "email", "value": "owner@example.com" },
    { "key": "password", "value": "your-password" },
    { "key": "phone", "value": "01712345678" },
    { "key": "token", "value": "" },
    { "key": "otp_id", "value": "" },
    { "key": "code", "value": "" }
  ],
  "item": [
    {
      "name": "1. Login (saves token)",
      "event": [{
        "listen": "test",
        "script": { "type": "text/javascript", "exec": [
          "pm.test('Login OK', () => pm.response.to.have.status(200));",
          "const body = pm.response.json();",
          "if (body.token) { pm.collectionVariables.set('token', body.token); console.log('Token saved'); }"
        ]}
      }],
      "request": {
        "auth": { "type": "noauth" },
        "method": "POST",
        "header": [
          { "key": "Accept", "value": "application/json" },
          { "key": "Content-Type", "value": "application/json" }
        ],
        "body": { "mode": "raw", "raw": "{\n  \"email\": \"{{email}}\",\n  \"password\": \"{{password}}\"\n}" },
        "url": { "raw": "{{baseUrl}}/login", "host": ["{{baseUrl}}"], "path": ["login"] }
      }
    },
    {
      "name": "2. Send OTP (saves otp_id + code)",
      "event": [{
        "listen": "test",
        "script": { "type": "text/javascript", "exec": [
          "pm.test('OTP created (201)', () => pm.response.to.have.status(201));",
          "const body = pm.response.json();",
          "if (body.otp_id) pm.collectionVariables.set('otp_id', body.otp_id);",
          "if (body.code) pm.collectionVariables.set('code', body.code);",
          "console.log('otp_id =', body.otp_id, ' code =', body.code);"
        ]}
      }],
      "request": {
        "method": "POST",
        "header": [
          { "key": "Accept", "value": "application/json" },
          { "key": "Content-Type", "value": "application/json" }
        ],
        "body": { "mode": "raw", "raw": "{\n  \"phone_number\": \"{{phone}}\",\n  \"ttl_minutes\": 10\n}" },
        "url": { "raw": "{{baseUrl}}/otp/send", "host": ["{{baseUrl}}"], "path": ["otp", "send"] },
        "description": "Omit `code` to let the server generate a 6-digit code (returned once in the response). To send your own code add \"code\": \"4729\" (4–8 digits); then set the `code` variable yourself for step 5."
      }
    },
    {
      "name": "3. OTP Status",
      "event": [{
        "listen": "test",
        "script": { "type": "text/javascript", "exec": [
          "pm.test('Status OK', () => pm.response.to.have.status(200));",
          "pm.test('Code is never returned', () => pm.expect(pm.response.json()).to.not.have.property('code'));"
        ]}
      }],
      "request": {
        "method": "GET",
        "header": [{ "key": "Accept", "value": "application/json" }],
        "url": { "raw": "{{baseUrl}}/otp/{{otp_id}}", "host": ["{{baseUrl}}"], "path": ["otp", "{{otp_id}}"] }
      }
    },
    {
      "name": "4. Verify OTP — wrong code (expect 422)",
      "event": [{
        "listen": "test",
        "script": { "type": "text/javascript", "exec": [
          "pm.test('Wrong code rejected (422)', () => pm.response.to.have.status(422));"
        ]}
      }],
      "request": {
        "method": "POST",
        "header": [
          { "key": "Accept", "value": "application/json" },
          { "key": "Content-Type", "value": "application/json" }
        ],
        "body": { "mode": "raw", "raw": "{\n  \"otp_id\": {{otp_id}},\n  \"code\": \"000000\"\n}" },
        "url": { "raw": "{{baseUrl}}/otp/verify", "host": ["{{baseUrl}}"], "path": ["otp", "verify"] },
        "description": "Send this 5 times to see the lock (423)."
      }
    },
    {
      "name": "5. Verify OTP — correct code (expect 200)",
      "event": [{
        "listen": "test",
        "script": { "type": "text/javascript", "exec": [
          "pm.test('Verified (200)', () => pm.response.to.have.status(200));",
          "pm.test('status = verified', () => pm.expect(pm.response.json().status).to.eql('verified'));"
        ]}
      }],
      "request": {
        "method": "POST",
        "header": [
          { "key": "Accept", "value": "application/json" },
          { "key": "Content-Type", "value": "application/json" }
        ],
        "body": { "mode": "raw", "raw": "{\n  \"otp_id\": {{otp_id}},\n  \"code\": \"{{code}}\"\n}" },
        "url": { "raw": "{{baseUrl}}/otp/verify", "host": ["{{baseUrl}}"], "path": ["otp", "verify"] },
        "description": "Run again after success to see 409 (already verified)."
      }
    }
  ]
}
