import {describe, expect, beforeEach} from 'vitest'
import {createStore} from 'vuex'
import {store} from '@store/modules/proxy/index.js'
import {addedNewRoute, updatedRoute, sortedRoutes, deletedRoute} from '@store/modules/proxy/helpers.js'

const selectedSite = store.state.selectedSite
const routes = store.state.routes
const routesState = store.state.routesState

const setSelectedSite = store.mutations.setSelectedSite
const setRoutes = store.mutations.setRoutes
const setRoutesState = store.mutations.setRoutesState

const addNewRouteLayout = store.actions.addNewRouteLayout
const uploadSiteRoutes = store.actions.uploadSiteRoutes
const updateRouteRow = store.actions.updateRouteRow
const updateRoutesWithApi = store.actions.updateRoutesWithApi
const removeRoute = store.actions.removeRoute
const resetStore = store.actions.resetStore

const defaultRoutes = [{
  "id": 7,
  "created_at": "2024-02-27T12:02:34.666042252+03:00",
  "updated_at": "2024-02-27T12:02:34.666042252+03:00",
  "deleted_at": null,
  "server_id": -1,
  "path": "/route",
  "role": 0,
  "description": "with route",
  "order": 0,
  "deepness": 0,
  "is_cb_on": true,
  "cb_request_limit": 0,
  "cb_min_requests": 7,
  "cb_error_threshold_percentage": 0,
  "cb_interval_duration": 0,
  "cb_open_state_timeout": 0,
  "is_online": false
},
{
  "id": 8,
  "created_at": "2024-02-27T12:05:45.268921795+03:00",
  "updated_at": "2024-02-27T12:05:45.268921795+03:00",
  "deleted_at": null,
  "server_id": 7,
  "path": "new/3",
  "role": 0,
  "description": "",
  "order": 0,
  "deepness": 0,
  "is_cb_on": false,
  "cb_request_limit": 3,
  "cb_min_requests": 5,
  "cb_error_threshold_percentage": 0,
  "cb_interval_duration": 0,
  "cb_open_state_timeout": 0,
  "is_online": true
},
{
  "id": 9,
  "created_at": "2024-02-27T12:11:09.940033992+03:00",
  "updated_at": "2024-02-27T12:11:09.940033992+03:00",
  "deleted_at": null,
  "server_id": -1,
  "path": "new/1",
  "role": 0,
  "description": "",
  "order": 0,
  "deepness": 9,
  "is_cb_on": true,
  "cb_request_limit": 0,
  "cb_min_requests": 0,
  "cb_error_threshold_percentage": 0,
  "cb_interval_duration": 0,
  "cb_open_state_timeout": 5,
  "is_online": false
}]

const mockStore = createStore({
  state: {
    selectedSite: selectedSite,
    routes: routes,
    routesState: routesState,
  },
  getters: {
    selectedSite: (state) => state.selectedSite,
  },
  mutations: {
    setSelectedSite: setSelectedSite,
    setRoutes: setRoutes,
    setRoutesState: setRoutesState
  },
  actions: {
    addNewRouteLayout: addNewRouteLayout,
    uploadSiteRoutes: uploadSiteRoutes,
    updateRouteRow: updateRouteRow,
    updateRoutesWithApi: updateRoutesWithApi,
    removeRoute: removeRoute,
    resetStore
  },
})

describe('actions', () => {
  //eslint-disable-next-line no-undef
  it('action addNewRouteLayout - added fields for new Route', async () => {
    beforeEach(() => {
      mockStore.dispatch('resetStore')
    })
    mockStore.commit('setSelectedSite', {id: 9}) // added id for selected site, need to this action for bind server_id
    mockStore.dispatch('addNewRouteLayout')
    expect(mockStore.state.routes[0]).toMatchObject({action: "create", server_id: 9})
  })
  //eslint-disable-next-line no-undef
  it('action updateRouteRow - edited fields values, in selected row', async () => {
    beforeEach(() => {
      mockStore.dispatch('resetStore')
    })
    
    const editedRoute = {
      "id": 8,
      "created_at": "2024-02-27T12:05:45.268921795+03:00",
      "updated_at": "2024-02-27T12:05:45.268921795+03:00",
      "deleted_at": null,
      "server_id": 7,
      "path": "new/3",
      "role": 0,
      "description": "Testing edited route",
      "order": 0,
      "deepness": 0,
      "is_cb_on": true,
      "cb_request_limit": 3,
      "cb_min_requests": 5,
      "cb_error_threshold_percentage": 0,
      "cb_interval_duration": 7,
      "cb_open_state_timeout": 0,
      "is_online": true
    }
    mockStore.commit('setRoutes', defaultRoutes)
    const currentRoute = mockStore.state.routes.find(route => route.id === 8)
    expect(currentRoute).toMatchObject({"description": "", "is_cb_on": false, "cb_interval_duration": 0}) // check default values fields in selected route
    mockStore.dispatch('updateRouteRow', editedRoute)
    const updatedRoute = mockStore.state.routes.find(route => route.id === 8)
    expect(updatedRoute).toMatchObject({"description": "Testing edited route", "is_cb_on": true, "cb_interval_duration": 7})
  })
  //eslint-disable-next-line no-undef
  it('action updateRouteRow - edited fields, empty values, in selected row', async () => {
    beforeEach(() => {
      mockStore.dispatch('resetStore')
    })
    
    const editedRoute = {
      "id": 8,
      "created_at": "2024-02-27T12:05:45.268921795+03:00",
      "updated_at": "2024-02-27T12:05:45.268921795+03:00",
      "deleted_at": null,
      "server_id": 7,
      "path": "",
      "role": 0,
      "description": null,
      "order": 0,
      "deepness": 0,
      "is_cb_on": true,
      "cb_request_limit": 0,
      "cb_min_requests": 5,
      "cb_error_threshold_percentage": 0,
      "cb_interval_duration": 7,
      "cb_open_state_timeout": 0,
      "is_online": true
    }
    mockStore.commit('setRoutes', defaultRoutes)
    const currentRoute = mockStore.state.routes.find(route => route.id === 8)
    expect(currentRoute).toMatchObject({"cb_request_limit": 3, "path": "new/3", "description": ""}) // check default values fields in selected route
    mockStore.dispatch('updateRouteRow', editedRoute)
    const updatedRoute = mockStore.state.routes.find(route => route.id === 8)
    expect(updatedRoute).toMatchObject({"cb_request_limit": 0, "path": "", "description": null})
  })

})

describe('helpers', () => {
  //eslint-disable-next-line no-undef
  it('added new route to site', async () => {
    beforeEach(() => {
      mockStore.dispatch('resetStore')
    })

    const editedSite = {
      "id": 3,
      "created_at": "2024-02-26T17:50:07.191225008+03:00",
      "updated_at": "2024-02-28T09:41:49.274089436+03:00",
      "deleted_at": null,
      "name": "new",
      "port": 3645,
      "proxy_ip": "172.25.78.151",
      "site_ip": "172.25.78.151",
      "internet_uri": "",
      "description": "new updated...",
      "is_online": false
    }

    const newRoute = {
      'action': "create",
      "path": "",
      "role": 0,
      "description": "9",
      "order": 0,
      "deepness": 7,
      "is_cb_on": false,
      "cb_request_limit": 0,
      "cb_min_requests": 0,
      "cb_error_threshold_percentage": 0,
      "cb_interval_duration": 3,
      "cb_open_state_timeout": 0,
      "is_online": true
    }

    mockStore.commit('setRoutes', [newRoute, ...defaultRoutes])
    const updatedRoutes = addedNewRoute(editedSite, mockStore.state.routes)
    mockStore.commit('setRoutes', updatedRoutes)
    const addedNewRouteToStore = mockStore.state.routes.find(route => route.action === 'create')
    expect(addedNewRouteToStore).not.toBe(undefined)
    expect(editedSite.id).toEqual(addedNewRouteToStore.server_id) // if true then added new route successfully
  })
  //eslint-disable-next-line no-undef
  it('updated route', async () => {
    beforeEach(() => {
      mockStore.dispatch('resetStore')
    })

    const editedRoute = {
      "id": 8,
      "created_at": "2024-02-27T12:05:45.268921795+03:00",
      "updated_at": "2024-02-27T12:05:45.268921795+03:00",
      "deleted_at": null,
      "server_id": 7,
      "path": "new/3",
      "role": 0,
      "description": "",
      "order": 0,
      "deepness": 9,
      "is_cb_on": true,
      "cb_request_limit": 3,
      "cb_min_requests": 5,
      "cb_error_threshold_percentage": 0,
      "cb_interval_duration": 7,
      "cb_open_state_timeout": 0,
      "is_online": true
    }

    mockStore.commit('setRoutes', defaultRoutes)
    const currentRouteBeforeEdit = mockStore.state.routes.find(route => route.id === 8)
    expect(currentRouteBeforeEdit).toMatchObject({"deepness": 0, is_cb_on: false, "cb_interval_duration": 0}) // default params route
    const updatedRoutes = updatedRoute(editedRoute, mockStore.state.routes)
    mockStore.commit('setRoutes', updatedRoutes)
    const updatedRouteToStore = mockStore.state.routes.find(route => route.id === 8)
    expect(updatedRouteToStore).not.toBe(undefined)
    expect(updatedRouteToStore).toMatchObject({"deepness": 9, is_cb_on: true, "cb_interval_duration": 7}) // check new params updated route
  })
  //eslint-disable-next-line no-undef
  it('deleted route', async () => {
    beforeEach(() => {
      mockStore.dispatch('resetStore')
    })

    const deleteRouteId = 9

    mockStore.commit('setRoutes', defaultRoutes)
    const currentRouteBeforeDelete = mockStore.state.routes.find(route => route.id === 9)
    expect(currentRouteBeforeDelete).toMatchObject({id: 9}) // exists route before deleteting
    const updatedRoutes = deletedRoute(deleteRouteId, mockStore.state.routes)
    mockStore.commit('setRoutes', updatedRoutes)
    const deletedRouteToStore = mockStore.state.routes.find(route => route.id === 9)
    expect(deletedRouteToStore).toMatchObject({action: 'remove'}) // to deleted route to be added prop action with value 'remove'
  })
  //eslint-disable-next-line no-undef
  it('sorted routes - sort in descending order, sorted prop: updated_at', async () => {
    beforeEach(() => {
      mockStore.dispatch('resetStore')
    })

    const newRoute = {
      "id": 10,
      "created_at": "2024-02-27T12:12:53.295785444+03:00",
      "updated_at": "2024-02-27T12:12:53.295785444+03:00",
      "deleted_at": null,
      "server_id": -1,
      "path": "new/12",
      "role": 0,
      "description": "",
      "order": 1,
      "deepness": 0,
      "is_cb_on": false,
      "cb_request_limit": 3,
      "cb_min_requests": 5,
      "cb_error_threshold_percentage": 0,
      "cb_interval_duration": 0,
      "cb_open_state_timeout": 0,
      "is_online": true
    }

    mockStore.commit('setRoutes', [...defaultRoutes, newRoute])
    const newRouteIdxBeforeSorting = mockStore.state.routes.findIndex(route => route.id === 10)
    expect(newRouteIdxBeforeSorting).toBe(3) // default last index this new route in test
    const updatedRoutes = sortedRoutes(mockStore.state.routes)
    mockStore.commit('setRoutes', updatedRoutes)
    const newRouteIdxAfterSorting = mockStore.state.routes.findIndex(route => route.id === 10)
    expect(newRouteIdxAfterSorting).toBe(0) // check route with id 10 moved to first index after sorting, because new route have updated_at === created_at for nre routes
  })

})