{
    "schemes": [],
    "swagger": "2.0",
    "info": {
        "description": "Invoice billing service for Banknote Tracker",
        "title": "BNT Invoice Service API",
        "contact": {},
        "version": "1.0"
    },
    "host": "localhost:9011",
    "basePath": "/",
    "paths": {
        "/invoices": {
            "get": {
                "security": [
                    {
                        "BearerAuth": []
                    }
                ],
                "description": "List invoices with pagination, sorting, and filtering",
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "invoices"
                ],
                "summary": "List invoices",
                "parameters": [
                    {
                        "type": "integer",
                        "description": "Page number",
                        "name": "page",
                        "in": "query"
                    },
                    {
                        "type": "string",
                        "description": "Sort by field",
                        "name": "sort_by",
                        "in": "query"
                    },
                    {
                        "type": "string",
                        "description": "Order by (asc or desc)",
                        "name": "order_by",
                        "in": "query"
                    },
                    {
                        "type": "string",
                        "description": "Search by bill number",
                        "name": "search",
                        "in": "query"
                    },
                    {
                        "type": "string",
                        "description": "Filter by creation date range start (RFC3339 or YYYY-MM-DD)",
                        "name": "from",
                        "in": "query"
                    },
                    {
                        "type": "string",
                        "description": "Filter by creation date range end (RFC3339 or YYYY-MM-DD)",
                        "name": "to",
                        "in": "query"
                    },
                    {
                        "type": "integer",
                        "description": "Filter by invoice year (matches week_ended between YYYY-01-01 and YYYY-12-31)",
                        "name": "year",
                        "in": "query"
                    },
                    {
                        "type": "string",
                        "description": "Filter by week_ended range start (YYYY-MM-DD)",
                        "name": "week_from",
                        "in": "query"
                    },
                    {
                        "type": "string",
                        "description": "Filter by week_ended range end (YYYY-MM-DD)",
                        "name": "week_to",
                        "in": "query"
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/schemas.PaginatedInvoiceResponse"
                        }
                    },
                    "500": {
                        "description": "Internal Server Error",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    }
                }
            },
            "post": {
                "security": [
                    {
                        "BearerAuth": []
                    }
                ],
                "description": "Create a new invoice",
                "consumes": [
                    "application/json"
                ],
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "invoices"
                ],
                "summary": "Create invoice",
                "parameters": [
                    {
                        "description": "Create invoice request",
                        "name": "request",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/schemas.CreateInvoiceRequest"
                        }
                    }
                ],
                "responses": {
                    "201": {
                        "description": "Created",
                        "schema": {
                            "$ref": "#/definitions/schemas.ReferenceResponse"
                        }
                    },
                    "400": {
                        "description": "Bad Request",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "401": {
                        "description": "Unauthorized",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "403": {
                        "description": "Forbidden",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "500": {
                        "description": "Internal Server Error",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    }
                }
            }
        },
        "/invoices/by-bill-no/{bill_no}": {
            "get": {
                "security": [
                    {
                        "BearerAuth": []
                    }
                ],
                "description": "Retrieve an invoice by its bill number",
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "invoices"
                ],
                "summary": "Get invoice by bill number",
                "parameters": [
                    {
                        "type": "string",
                        "description": "Bill number",
                        "name": "bill_no",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/schemas.InvoiceResponse"
                        }
                    },
                    "404": {
                        "description": "Not Found",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "500": {
                        "description": "Internal Server Error",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    }
                }
            }
        },
        "/invoices/by-case/{case_reference}": {
            "get": {
                "security": [
                    {
                        "BearerAuth": []
                    }
                ],
                "description": "Reverse-lookup: given a case reference (UUID from the prefix\nservice), return the invoice that contains that case in any\nof its line items' case_references arrays. Use this to answer\n\"which invoice did this case end up in?\" without scanning\nevery invoice manually. Returns 404 if no invoice contains\nthe case (e.g. the case was reverted out of invoiced status,\nor it was never invoiced in the first place).",
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "invoices"
                ],
                "summary": "Get invoice by case reference",
                "parameters": [
                    {
                        "type": "string",
                        "description": "Case reference (UUID)",
                        "name": "case_reference",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/schemas.InvoiceResponse"
                        }
                    },
                    "400": {
                        "description": "Bad Request",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "404": {
                        "description": "Not Found",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "500": {
                        "description": "Internal Server Error",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    }
                }
            }
        },
        "/invoices/health": {
            "get": {
                "description": "Returns service health status",
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "health"
                ],
                "summary": "Health check",
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/schemas.HealthResponse"
                        }
                    }
                }
            }
        },
        "/invoices/next-bill-no": {
            "get": {
                "security": [
                    {
                        "BearerAuth": []
                    }
                ],
                "description": "Get the next available bill number",
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "invoices"
                ],
                "summary": "Get next bill number",
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/schemas.NextBillNoResponse"
                        }
                    },
                    "500": {
                        "description": "Internal Server Error",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    }
                }
            }
        },
        "/invoices/reports/invoice/{reference}": {
            "get": {
                "security": [
                    {
                        "BearerAuth": []
                    }
                ],
                "description": "Returns enriched invoice data for PDF report rendering, including derived financial fields (VAT rate, advance rate, payable breakdown) and resolved organisation name",
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "reports"
                ],
                "summary": "Invoice report data",
                "parameters": [
                    {
                        "type": "string",
                        "description": "Invoice reference",
                        "name": "reference",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "type": "object",
                            "additionalProperties": true
                        }
                    },
                    "404": {
                        "description": "Not Found",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "500": {
                        "description": "Internal Server Error",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    }
                }
            }
        },
        "/invoices/{reference}": {
            "get": {
                "security": [
                    {
                        "BearerAuth": []
                    }
                ],
                "description": "Retrieve an invoice by its reference",
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "invoices"
                ],
                "summary": "Get invoice by reference",
                "parameters": [
                    {
                        "type": "string",
                        "description": "Invoice reference",
                        "name": "reference",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/schemas.InvoiceResponse"
                        }
                    },
                    "404": {
                        "description": "Not Found",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "500": {
                        "description": "Internal Server Error",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    }
                }
            },
            "put": {
                "security": [
                    {
                        "BearerAuth": []
                    }
                ],
                "description": "Update an existing invoice by reference",
                "consumes": [
                    "application/json"
                ],
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "invoices"
                ],
                "summary": "Update invoice",
                "parameters": [
                    {
                        "type": "string",
                        "description": "Invoice reference",
                        "name": "reference",
                        "in": "path",
                        "required": true
                    },
                    {
                        "description": "Update invoice request",
                        "name": "request",
                        "in": "body",
                        "required": true,
                        "schema": {
                            "$ref": "#/definitions/schemas.UpdateInvoiceRequest"
                        }
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/schemas.ReferenceResponse"
                        }
                    },
                    "400": {
                        "description": "Bad Request",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "401": {
                        "description": "Unauthorized",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "403": {
                        "description": "Forbidden",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "404": {
                        "description": "Not Found",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "500": {
                        "description": "Internal Server Error",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    }
                }
            },
            "delete": {
                "security": [
                    {
                        "BearerAuth": []
                    }
                ],
                "description": "Delete an invoice by reference",
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "invoices"
                ],
                "summary": "Delete invoice",
                "parameters": [
                    {
                        "type": "string",
                        "description": "Invoice reference",
                        "name": "reference",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/schemas.ReferenceResponse"
                        }
                    },
                    "401": {
                        "description": "Unauthorized",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "403": {
                        "description": "Forbidden",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "404": {
                        "description": "Not Found",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "500": {
                        "description": "Internal Server Error",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    }
                }
            }
        },
        "/invoices/{reference}/disable": {
            "put": {
                "security": [
                    {
                        "BearerAuth": []
                    }
                ],
                "description": "Disable an invoice by reference",
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "invoices"
                ],
                "summary": "Disable invoice",
                "parameters": [
                    {
                        "type": "string",
                        "description": "Invoice reference",
                        "name": "reference",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/schemas.ReferenceResponse"
                        }
                    },
                    "401": {
                        "description": "Unauthorized",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "403": {
                        "description": "Forbidden",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "404": {
                        "description": "Not Found",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "500": {
                        "description": "Internal Server Error",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    }
                }
            }
        },
        "/invoices/{reference}/enable": {
            "put": {
                "security": [
                    {
                        "BearerAuth": []
                    }
                ],
                "description": "Enable an invoice by reference",
                "produces": [
                    "application/json"
                ],
                "tags": [
                    "invoices"
                ],
                "summary": "Enable invoice",
                "parameters": [
                    {
                        "type": "string",
                        "description": "Invoice reference",
                        "name": "reference",
                        "in": "path",
                        "required": true
                    }
                ],
                "responses": {
                    "200": {
                        "description": "OK",
                        "schema": {
                            "$ref": "#/definitions/schemas.ReferenceResponse"
                        }
                    },
                    "401": {
                        "description": "Unauthorized",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "403": {
                        "description": "Forbidden",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "404": {
                        "description": "Not Found",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    },
                    "500": {
                        "description": "Internal Server Error",
                        "schema": {
                            "$ref": "#/definitions/shared.ApplicationError"
                        }
                    }
                }
            }
        }
    },
    "definitions": {
        "schemas.AuditInfoResponse": {
            "type": "object",
            "properties": {
                "department_name": {
                    "type": "string"
                },
                "department_reference": {
                    "type": "string"
                },
                "device_name": {
                    "type": "string"
                },
                "email": {
                    "type": "string"
                },
                "full_name": {
                    "type": "string"
                },
                "ip_address": {
                    "type": "string"
                },
                "latitude": {
                    "type": "string"
                },
                "longitude": {
                    "type": "string"
                },
                "organisation_name": {
                    "type": "string"
                },
                "organisation_reference": {
                    "type": "string"
                },
                "photo_url": {
                    "type": "string"
                },
                "role": {
                    "type": "string"
                },
                "timestamp": {
                    "type": "string"
                },
                "user_reference": {
                    "type": "string"
                }
            }
        },
        "schemas.CreateInvoiceRequest": {
            "type": "object",
            "required": [
                "line_items",
                "week_ended"
            ],
            "properties": {
                "advance": {
                    "type": "number",
                    "minimum": 0
                },
                "line_items": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                        "$ref": "#/definitions/schemas.LineItemRequest"
                    }
                },
                "week_ended": {
                    "type": "string"
                }
            }
        },
        "schemas.HealthResponse": {
            "type": "object",
            "properties": {
                "message": {
                    "type": "string"
                }
            }
        },
        "schemas.InvoiceResponse": {
            "type": "object",
            "properties": {
                "advance": {
                    "type": "number"
                },
                "bill_no": {
                    "type": "string"
                },
                "created_by": {
                    "$ref": "#/definitions/schemas.AuditInfoResponse"
                },
                "department_reference": {
                    "type": "string"
                },
                "is_active": {
                    "type": "boolean"
                },
                "line_items": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/schemas.LineItemResponse"
                    }
                },
                "organisation_reference": {
                    "type": "string"
                },
                "payable": {
                    "type": "number"
                },
                "reference": {
                    "type": "string"
                },
                "total_value": {
                    "type": "number"
                },
                "updated_by": {
                    "$ref": "#/definitions/schemas.AuditInfoResponse"
                },
                "value": {
                    "type": "number"
                },
                "vat_on_value": {
                    "type": "number"
                },
                "week_ended": {
                    "type": "string"
                }
            }
        },
        "schemas.LineItemRequest": {
            "type": "object",
            "required": [
                "cases",
                "description",
                "unit_price"
            ],
            "properties": {
                "cases": {
                    "type": "integer"
                },
                "description": {
                    "type": "string"
                },
                "unit_price": {
                    "type": "number"
                }
            }
        },
        "schemas.LineItemResponse": {
            "type": "object",
            "properties": {
                "case_references": {
                    "type": "array",
                    "items": {
                        "type": "string"
                    }
                },
                "cases": {
                    "type": "integer"
                },
                "denomination_code": {
                    "type": "string"
                },
                "denomination_value": {
                    "type": "integer"
                },
                "description": {
                    "type": "string"
                },
                "quantity": {
                    "type": "integer"
                },
                "unit_price": {
                    "type": "number"
                },
                "value": {
                    "type": "number"
                }
            }
        },
        "schemas.NextBillNoResponse": {
            "type": "object",
            "properties": {
                "bill_no": {
                    "type": "string"
                }
            }
        },
        "schemas.PaginatedInvoiceResponse": {
            "type": "object",
            "properties": {
                "items": {
                    "type": "array",
                    "items": {
                        "$ref": "#/definitions/schemas.InvoiceResponse"
                    }
                },
                "pagination": {
                    "$ref": "#/definitions/schemas.Pagination"
                }
            }
        },
        "schemas.Pagination": {
            "type": "object",
            "properties": {
                "has_next": {
                    "type": "boolean"
                },
                "has_previous": {
                    "type": "boolean"
                },
                "page": {
                    "type": "integer"
                },
                "pages": {
                    "type": "integer"
                },
                "size": {
                    "type": "integer"
                },
                "total": {
                    "type": "integer"
                }
            }
        },
        "schemas.ReferenceResponse": {
            "type": "object",
            "properties": {
                "reference": {
                    "type": "string"
                }
            }
        },
        "schemas.UpdateInvoiceRequest": {
            "type": "object",
            "properties": {
                "advance": {
                    "type": "number",
                    "minimum": 0
                },
                "line_items": {
                    "type": "array",
                    "minItems": 1,
                    "items": {
                        "$ref": "#/definitions/schemas.LineItemRequest"
                    }
                },
                "week_ended": {
                    "type": "string"
                }
            }
        },
        "shared.ApplicationError": {
            "type": "object",
            "properties": {
                "error_reference": {
                    "type": "string"
                },
                "error_type": {
                    "$ref": "#/definitions/shared.ErrorType"
                },
                "errors": {
                    "type": "object",
                    "additionalProperties": {
                        "type": "array",
                        "items": {
                            "type": "string"
                        }
                    }
                },
                "status_code": {
                    "type": "integer"
                },
                "timestamp": {
                    "type": "string"
                }
            }
        },
        "shared.ErrorType": {
            "type": "string",
            "enum": [
                "VALIDATION_ERROR",
                "NOT_FOUND_ERROR",
                "CONFLICT_ERROR",
                "UNAUTHORIZED_ERROR",
                "AUTHENTICATION_ERROR",
                "FORBIDDEN_ERROR",
                "BAD_REQUEST_ERROR",
                "SERVER_ERROR",
                "REDIS_ERROR",
                "MONGO_DB_ERROR",
                "CREATE_ERROR",
                "UPDATE_ERROR",
                "INVALID_RESOURCE_ERROR",
                "TIMEOUT",
                "METHOD_NOT_ALLOWED",
                "RATE_LIMIT_EXCEEDED",
                "PAYLOAD_TOO_LARGE",
                "UNPROCESSABLE_ENTITY",
                "INTERNAL_ERROR"
            ],
            "x-enum-varnames": [
                "ValidationError",
                "NotFoundError",
                "ConflictError",
                "UnauthorizedError",
                "AuthenticationError",
                "ForbiddenError",
                "BadRequestError",
                "ServerError",
                "RedisError",
                "MongoDBError",
                "CreateError",
                "UpdateError",
                "InvalidResourceError",
                "TimeoutError",
                "MethodNotAllowedError",
                "RateLimitExceededError",
                "PayloadTooLargeError",
                "UnprocessableEntityError",
                "InternalError"
            ]
        }
    },
    "securityDefinitions": {
        "BearerAuth": {
            "type": "apiKey",
            "name": "Authorization",
            "in": "header"
        }
    }
}