import { expect, test, describe, vi, beforeEach} from "vitest"
import AdapterOfServices    from '@adapters/adapterOfServices/Services'
import ServiceOfServices from '@services/serviceOfServices/Services.js'
import { createStore } from 'vuex'
import {store as services} from "@/store/modules/services"
import axios from "axios";

vi.mock('axios')

const defaultServices = [
  {
    "id": 1,
    "created_at": "2024-03-06T17:31:31.948355541+03:00",
    "updated_at": "2024-03-06T17:31:31.948355541+03:00",
    "deleted_at": null,
    "name": "jsonplaceholder.typicode.com",
    "port": 9965,
    "proxy_ip": "172.25.78.153",
    "site_ip": "https://jsonplaceholder.typicode.com/",
    "internet_uri": "localhost",
    "description": "localhost",
    "is_online": true
  },
  {
    "id": 2,
    "created_at": "2024-03-07T11:43:51.026265459+03:00",
    "updated_at": "2024-03-07T13:35:12.506368972+03:00",
    "deleted_at": null,
    "name": "new 2",
    "port": 4548,
    "proxy_ip": "172.25.78.151",
    "site_ip": "172.25.78.151",
    "internet_uri": "",
    "description": "new site 2",
    "is_online": true
  },
  {
    "id": 3,
    "created_at": "2024-03-07T11:43:51.027148541+03:00",
    "updated_at": "2024-03-07T13:35:24.919273428+03:00",
    "deleted_at": null,
    "name": "new 3",
    "port": 2527,
    "proxy_ip": "172.25.78.151",
    "site_ip": "172.25.78.151",
    "internet_uri": "",
    "description": "new site 3...",
    "is_online": true
  }
]

const resServices = [
  {
    "id": 1,
    "created_at": "2024-03-06T17:31:31.948355541+03:00",
    "updated_at": "2024-03-06T17:31:31.948355541+03:00",
    "deleted_at": null,
    "name": "jsonplaceholder.typicode.com",
    "port": 9965,
    "proxy_ip": "172.25.78.153",
    "device_ip": "https://jsonplaceholder.typicode.com/",
    "internet_uri": "localhost",
    "description": "localhost",
    "is_online": true
  },
  {
    "id": 2,
    "created_at": "2024-03-07T11:43:51.026265459+03:00",
    "updated_at": "2024-03-07T13:35:12.506368972+03:00",
    "deleted_at": null,
    "name": "new 2",
    "port": 4548,
    "proxy_ip": "172.25.78.151",
    "device_ip": "172.25.78.151",
    "internet_uri": "",
    "description": "new site 2",
    "is_online": true
  },
  {
    "id": 3,
    "created_at": "2024-03-07T11:43:51.027148541+03:00",
    "updated_at": "2024-03-07T13:35:24.919273428+03:00",
    "deleted_at": null,
    "name": "new 3",
    "port": 2527,
    "proxy_ip": "172.25.78.151",
    "device_ip": "172.25.78.151",
    "internet_uri": "",
    "description": "new site 3...",
    "is_online": true
  }
]

describe("tests services store with vuex", () => {
  const store = createStore({
    plugins: [],
    modules: {
      services
    },
  })

  beforeEach(() => {
    store.dispatch('services/resetStore')
  })

  const adapterOfServices = new AdapterOfServices(import.meta.env.VITE_API_ADDR)
  const serviceOfServices = new ServiceOfServices(adapterOfServices, store)

  axios.get.mockResolvedValue({
    data: defaultServices,
  })

  test('Upload services', async () => {

    const services = store.getters['services/services']

    expect(services).toEqual([]) // empty services array of store

    await serviceOfServices.fetchServices()

    const uploadServices = store.getters['services/services']

    expect(uploadServices).toEqual(resServices) // full services array of store

  })

  test('Added new service layout to services of store', async () => {
    const expectedSiteData = {"port": "", "name": "", id: -1}

    await serviceOfServices.addNewServiceLayout()

    const selectedService = store.getters['services/selectedService']
    const services = store.getters['services/services']

    expect(
      {
        selectedService,
        services
      }
    ).toEqual({
      selectedService: expectedSiteData,
      services: [expectedSiteData]
    })
  })

  test('Selected service - for edit fields values in this service', async () => {
    
    const editedService = 
    {
      "id": 3,
      "created_at": "2024-03-07T11:43:51.027148541+03:00",
      "updated_at": "2024-03-07T13:35:24.919273428+03:00",
      "deleted_at": null,
      "name": "new 3",
      "port": 2527,
      "proxy_ip": "172.25.78.151",
      "device_ip": "172.25.78.151",
      "internet_uri": "",
      "description": "new site 3...",
      "is_online": true
    }

    await serviceOfServices.selectService(editedService)

    const selectedService = store.getters['services/selectedService']

    expect(selectedService).toEqual(editedService)

  })

  test('Edited fields values in selected service of store', async () => {
    
    const selectedForEditService = 
    {
      "id": 3,
      "created_at": "2024-03-07T11:43:51.027148541+03:00",
      "updated_at": "2024-03-07T13:35:24.919273428+03:00",
      "deleted_at": null,
      "name": "new 3",
      "port": 2527,
      "proxy_ip": "172.25.78.151",
      "device_ip": "172.25.78.151",
      "internet_uri": "",
      "description": "new site 3...",
      "is_online": true
    }
    
    await serviceOfServices.selectService(selectedForEditService)

    await serviceOfServices.editSelectedService({key: 'name', value: 'edited test name'}).then(async () => {
      await serviceOfServices.editSelectedService({key: 'port', value: 5555}).then(async () => {
        await serviceOfServices.editSelectedService({key: 'description', value: 'edited description field'})
      })
    })

    const selectedService = store.getters['services/selectedService']

    // console.log('Edited fields 4 test', selectedService)

    expect(selectedService.name).toEqual('edited test name')
    expect(selectedService.port).toEqual(5555)
    expect(selectedService.description).toEqual('edited description field')

  })

  // test("test uploadAndSelectService function", async () => {
  //   const mockData = [
  //     {
  //       "id": 1,
  //       "created_at": "2024-02-22T17:08:37.715772388+03:00",
  //       "updated_at": "2024-02-26T14:11:38.64094899+03:00",
  //       "deleted_at": null,
  //       "name": "jsonplaceholder.typicode.com",
  //       "port": 9965,
  //       "proxy_ip": "172.25.78.153",
  //       "site_ip": "172.25.78.153",
  //       "internet_uri": "localhost",
  //       "description": "localhost",
  //       "is_online": true
  //     }
  //   ]

  //   axios.get.mockResolvedValue({
  //     data: mockData,
  //   })

  //   await store.dispatch('services/uploadSites')
  //   store.dispatch('services/uploadAndSelectService', 1)

  //   const selectedSite = store.getters['services/selectedSite']
  //   const selectedSiteState = store.getters['services/selectedSiteState']

  //   expect(selectedSite).toEqual({
  //     "id": 1,
  //     "created_at": "2024-02-22T17:08:37.715772388+03:00",
  //     "updated_at": "2024-02-26T14:11:38.64094899+03:00",
  //     "deleted_at": null,
  //     "name": "jsonplaceholder.typicode.com",
  //     "port": 9965,
  //     "proxy_ip": "172.25.78.153",
  //     "device_ip": "172.25.78.153",
  //     "internet_uri": "localhost",
  //     "description": "localhost",
  //     "is_online": true
  //   })
  //   expect(selectedSiteState).toEqual('active')
  // })

})


