import os

from fastapi import APIRouter, Depends
from fastapi.responses import FileResponse
from services.amazon.downloaders import NotUsProfit, UsProfit
from services.amazon.stores import (CanadaStores, EuropeanUnionStores,
                                    MexicoStores, UnitedKingdomStores,
                                    UnitedStatesStores)
from services.database.user_database import UserDatabase
from services.pandi import Pandi
from services.schemas import User

amazon = APIRouter(prefix="/amazon")
services = UserDatabase()


@amazon.get("/united_states")
async def amazon_united_states(
        start_date: str,
        end_date: str,
        current_user: User = Depends(services.get_current_user),
):
    downloader = UsProfit(
        start_date,
        end_date,
        stores=UnitedStatesStores,
        store="AmazonUnitedStates",
    )
    orders = await downloader.gross_sale()
    refunds = await downloader.gross_refund()
    adjustement = await downloader.gross_adjustement()

    Pandi.save_xlsx(orders, "united_states_orders")
    Pandi.save_xlsx(refunds, "united_states_refunds")
    Pandi.save_xlsx(adjustement, "united_states_adjustement")

    return {
        "orders": orders,
        "refunds": refunds,
        "adjustement": adjustement,
    }


@amazon.get("/canada")
async def canada(
        start_date: str,
        end_date: str,
        current_user: User = Depends(services.get_current_user),
):

    downloader = NotUsProfit(start_date,
                             end_date,
                             stores=CanadaStores,
                             store="AmazonCanada")

    orders = await downloader.gross_sale()
    refunds = await downloader.gross_refund()
    adjustement = await downloader.gross_adjustement()

    Pandi.save_xlsx(orders, "canada_orders")
    Pandi.save_xlsx(refunds, "canada_refunds")
    Pandi.save_xlsx(adjustement, "canada_adjustement")

    return {
        "orders": orders,
        "refunds": refunds,
        "adjustement": adjustement,
    }


@amazon.get("/{store}/download/{type}")
async def download(
        store: str,
        type: str,
        current_user: User = Depends(services.get_current_user),
):

    path: str = os.path.abspath(f"apps/static/reports/{store}_{type}.xlsx")
    filename: str = f"{store}_{type}.xlsx"
    return FileResponse(
        path=path,
        filename=filename,
        media_type=
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    )


@amazon.get("/united_kingdom")
async def united_kingdom(
        start_date: str,
        end_date: str,
        current_user: User = Depends(services.get_current_user),
):

    downloader = NotUsProfit(start_date,
                             end_date,
                             stores=UnitedKingdomStores,
                             store="AmazonUnitedKingdom")

    orders = await downloader.gross_sale()
    refunds = await downloader.gross_refund()
    adjustement = await downloader.gross_adjustement()

    Pandi.save_xlsx(orders, "united_kingdom_orders")
    Pandi.save_xlsx(refunds, "united_kingdom_refunds")
    Pandi.save_xlsx(adjustement, "united_kingdom_adjustement")

    return {
        "orders": orders,
        "refunds": refunds,
        "adjustement": adjustement,
    }


@amazon.get("/european_union")
async def european_union(
        start_date: str,
        end_date: str,
        current_user: User = Depends(services.get_current_user),
):

    downloader = NotUsProfit(start_date,
                             end_date,
                             stores=EuropeanUnionStores,
                             store="AmazonEuropeanUnion")

    orders = await downloader.gross_sale()
    refunds = await downloader.gross_refund()
    adjustement = await downloader.gross_adjustement()

    Pandi.save_xlsx(orders, "european_union_orders")
    Pandi.save_xlsx(refunds, "european_union_refunds")
    Pandi.save_xlsx(adjustement, "european_union_adjustement")

    return {
        "orders": orders,
        "refunds": refunds,
        "adjustement": adjustement,
    }


@amazon.get("/mexico")
async def mexico(
        start_date: str,
        end_date: str,
        current_user: User = Depends(services.get_current_user),
):

    downloader = NotUsProfit(start_date,
                             end_date,
                             stores=MexicoStores,
                             store="AmazonMexico")

    orders = await downloader.gross_sale()
    refunds = await downloader.gross_refund()
    adjustement = await downloader.gross_adjustement()

    Pandi.save_xlsx(orders, "mexico_orders")
    Pandi.save_xlsx(refunds, "mexico_refunds")
    Pandi.save_xlsx(adjustement, "mexico_adjustement")

    return {
        "orders": orders,
        "refunds": refunds,
        "adjustement": adjustement,
    }

import os

from fastapi import APIRouter, Depends
from fastapi.responses import FileResponse
from services.amazon.downloaders import NotUsProfit, UsProfit
from services.amazon.stores import (CanadaStores, EuropeanUnionStores,
                                    MexicoStores, UnitedKingdomStores,
                                    UnitedStatesStores)
from services.database.user_database import UserDatabase
from services.pandi import Pandi
from services.schemas import User

amazon = APIRouter(prefix="/amazon")
services = UserDatabase()


@amazon.get("/united_states")
async def amazon_united_states(
        start_date: str,
        end_date: str,
        current_user: User = Depends(services.get_current_user),
):
    downloader = UsProfit(
        start_date,
        end_date,
        stores=UnitedStatesStores,
        store="AmazonUnitedStates",
    )
    orders = await downloader.gross_sale()
    refunds = await downloader.gross_refund()
    adjustement = await downloader.gross_adjustement()

    Pandi.save_xlsx(orders, "united_states_orders")
    Pandi.save_xlsx(refunds, "united_states_refunds")
    Pandi.save_xlsx(adjustement, "united_states_adjustement")

    return {
        "orders": orders,
        "refunds": refunds,
        "adjustement": adjustement,
    }


@amazon.get("/canada")
async def canada(
        start_date: str,
        end_date: str,
        current_user: User = Depends(services.get_current_user),
):

    downloader = NotUsProfit(start_date,
                             end_date,
                             stores=CanadaStores,
                             store="AmazonCanada")

    orders = await downloader.gross_sale()
    refunds = await downloader.gross_refund()
    adjustement = await downloader.gross_adjustement()

    Pandi.save_xlsx(orders, "canada_orders")
    Pandi.save_xlsx(refunds, "canada_refunds")
    Pandi.save_xlsx(adjustement, "canada_adjustement")

    return {
        "orders": orders,
        "refunds": refunds,
        "adjustement": adjustement,
    }


@amazon.get("/{store}/download/{type}")
async def download(
        store: str,
        type: str,
        current_user: User = Depends(services.get_current_user),
):

    path: str = os.path.abspath(f"apps/static/reports/{store}_{type}.xlsx")
    filename: str = f"{store}_{type}.xlsx"
    return FileResponse(
        path=path,
        filename=filename,
        media_type=
        "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
    )


@amazon.get("/united_kingdom")
async def united_kingdom(
        start_date: str,
        end_date: str,
        current_user: User = Depends(services.get_current_user),
):

    downloader = NotUsProfit(start_date,
                             end_date,
                             stores=UnitedKingdomStores,
                             store="AmazonUnitedKingdom")

    orders = await downloader.gross_sale()
    refunds = await downloader.gross_refund()
    adjustement = await downloader.gross_adjustement()

    Pandi.save_xlsx(orders, "united_kingdom_orders")
    Pandi.save_xlsx(refunds, "united_kingdom_refunds")
    Pandi.save_xlsx(adjustement, "united_kingdom_adjustement")

    return {
        "orders": orders,
        "refunds": refunds,
        "adjustement": adjustement,
    }


@amazon.get("/european_union")
async def european_union(
        start_date: str,
        end_date: str,
        current_user: User = Depends(services.get_current_user),
):

    downloader = NotUsProfit(start_date,
                             end_date,
                             stores=EuropeanUnionStores,
                             store="AmazonEuropeanUnion")

    orders = await downloader.gross_sale()
    refunds = await downloader.gross_refund()
    adjustement = await downloader.gross_adjustement()

    Pandi.save_xlsx(orders, "european_union_orders")
    Pandi.save_xlsx(refunds, "european_union_refunds")
    Pandi.save_xlsx(adjustement, "european_union_adjustement")

    return {
        "orders": orders,
        "refunds": refunds,
        "adjustement": adjustement,
    }


@amazon.get("/mexico")
async def mexico(
        start_date: str,
        end_date: str,
        current_user: User = Depends(services.get_current_user),
):

    downloader = NotUsProfit(start_date,
                             end_date,
                             stores=MexicoStores,
                             store="AmazonMexico")

    orders = await downloader.gross_sale()
    refunds = await downloader.gross_refund()
    adjustement = await downloader.gross_adjustement()

    Pandi.save_xlsx(orders, "mexico_orders")
    Pandi.save_xlsx(refunds, "mexico_refunds")
    Pandi.save_xlsx(adjustement, "mexico_adjustement")

    return {
        "orders": orders,
        "refunds": refunds,
        "adjustement": adjustement,
    }
