from fastapi import APIRouter, Depends, HTTPException
from sqlalchemy.orm import Session

from dispatch.database.core import get_db
from dispatch.database.service import common_parameters, search_filter_sort_paginate
from dispatch.auth.permissions import SensitiveProjectActionPermission, PermissionsDependency

from .models import (
    IncidentCostCreate,
    IncidentCostPagination,
    IncidentCostRead,
    IncidentCostUpdate,
)
from .service import create, delete, get, update


router = APIRouter()


@router.get("", response_model=IncidentCostPagination)
def get_incident_costs(*, common: dict = Depends(common_parameters)):
    """
    Get all incident costs, or only those matching a given search term.
    """
    return search_filter_sort_paginate(model="IncidentCost", **common)


@router.get("/{incident_cost_id}", response_model=IncidentCostRead)
def get_incident_cost(*, db_session: Session = Depends(get_db), incident_cost_id: int):
    """
    Get an incident cost by id.
    """
    incident_cost = get(db_session=db_session, incident_cost_id=incident_cost_id)
    if not incident_cost:
        raise HTTPException(status_code=404, detail="An incident cost with this id does not exist.")
    return incident_cost


@router.post(
    "",
    response_model=IncidentCostRead,
    dependencies=[Depends(PermissionsDependency([SensitiveProjectActionPermission]))],
)
def create_incident_cost(
    *, db_session: Session = Depends(get_db), incident_cost_in: IncidentCostCreate
):
    """
    Create an incident cost.
    """
    incident_cost = create(db_session=db_session, incident_cost_in=incident_cost_in)
    return incident_cost


@router.put(
    "/{incident_cost_id}",
    response_model=IncidentCostRead,
    dependencies=[Depends(PermissionsDependency([SensitiveProjectActionPermission]))],
)
def update_incident_cost(
    *,
    db_session: Session = Depends(get_db),
    incident_cost_id: int,
    incident_cost_in: IncidentCostUpdate,
):
    """
    Update an incident cost by id.
    """
    incident_cost = get(db_session=db_session, incident_cost_id=incident_cost_id)
    if not incident_cost:
        raise HTTPException(status_code=404, detail="An incident cost with this id does not exist.")
    incident_cost = update(
        db_session=db_session,
        incident_cost=incident_cost,
        incident_cost_in=incident_cost_in,
    )
    return incident_cost


@router.delete(
    "/{incident_cost_id}",
    dependencies=[Depends(PermissionsDependency([SensitiveProjectActionPermission]))],
)
def delete_incident_cost(*, db_session: Session = Depends(get_db), incident_cost_id: int):
    """
    Delete an incident cost, returning only an HTTP 200 OK if successful.
    """
    incident_cost = get(db_session=db_session, incident_cost_id=incident_cost_id)
    if not incident_cost:
        raise HTTPException(status_code=404, detail="An incident cost with this id does not exist.")
    delete(db_session=db_session, incident_cost_id=incident_cost_id)

[((494, 505), 'fastapi.APIRouter', 'APIRouter', ([], {}), '()\n', (503, 505), False, 'from fastapi import APIRouter, Depends, HTTPException\n'), ((604, 630), 'fastapi.Depends', 'Depends', (['common_parameters'], {}), '(common_parameters)\n', (611, 630), False, 'from fastapi import APIRouter, Depends, HTTPException\n'), ((732, 791), 'dispatch.database.service.search_filter_sort_paginate', 'search_filter_sort_paginate', ([], {'model': '"""IncidentCost"""'}), "(model='IncidentCost', **common)\n", (759, 791), False, 'from dispatch.database.service import common_parameters, search_filter_sort_paginate\n'), ((909, 924), 'fastapi.Depends', 'Depends', (['get_db'], {}), '(get_db)\n', (916, 924), False, 'from fastapi import APIRouter, Depends, HTTPException\n'), ((1437, 1452), 'fastapi.Depends', 'Depends', (['get_db'], {}), '(get_db)\n', (1444, 1452), False, 'from fastapi import APIRouter, Depends, HTTPException\n'), ((1876, 1891), 'fastapi.Depends', 'Depends', (['get_db'], {}), '(get_db)\n', (1883, 1891), False, 'from fastapi import APIRouter, Depends, HTTPException\n'), ((2579, 2594), 'fastapi.Depends', 'Depends', (['get_db'], {}), '(get_db)\n', (2586, 2594), False, 'from fastapi import APIRouter, Depends, HTTPException\n'), ((1120, 1211), 'fastapi.HTTPException', 'HTTPException', ([], {'status_code': '(404)', 'detail': '"""An incident cost with this id does not exist."""'}), "(status_code=404, detail=\n    'An incident cost with this id does not exist.')\n", (1133, 1211), False, 'from fastapi import APIRouter, Depends, HTTPException\n'), ((2138, 2229), 'fastapi.HTTPException', 'HTTPException', ([], {'status_code': '(404)', 'detail': '"""An incident cost with this id does not exist."""'}), "(status_code=404, detail=\n    'An incident cost with this id does not exist.')\n", (2151, 2229), False, 'from fastapi import APIRouter, Depends, HTTPException\n'), ((2832, 2923), 'fastapi.HTTPException', 'HTTPException', ([], {'status_code': '(404)', 'detail': '"""An incident cost with this id does not exist."""'}), "(status_code=404, detail=\n    'An incident cost with this id does not exist.')\n", (2845, 2923), False, 'from fastapi import APIRouter, Depends, HTTPException\n'), ((1319, 1376), 'dispatch.auth.permissions.PermissionsDependency', 'PermissionsDependency', (['[SensitiveProjectActionPermission]'], {}), '([SensitiveProjectActionPermission])\n', (1340, 1376), False, 'from dispatch.auth.permissions import SensitiveProjectActionPermission, PermissionsDependency\n'), ((1754, 1811), 'dispatch.auth.permissions.PermissionsDependency', 'PermissionsDependency', (['[SensitiveProjectActionPermission]'], {}), '([SensitiveProjectActionPermission])\n', (1775, 1811), False, 'from dispatch.auth.permissions import SensitiveProjectActionPermission, PermissionsDependency\n'), ((2466, 2523), 'dispatch.auth.permissions.PermissionsDependency', 'PermissionsDependency', (['[SensitiveProjectActionPermission]'], {}), '([SensitiveProjectActionPermission])\n', (2487, 2523), False, 'from dispatch.auth.permissions import SensitiveProjectActionPermission, PermissionsDependency\n')]