using System.Collections.Generic;
using System.Text.Json;
using System.Threading.Tasks;

public class EmployeeService
{
    private readonly OdooService _odooService;

    public EmployeeService(OdooService odooService)
    {
        _odooService = odooService;
    }

    public async Task<int> CreateEmployeeAsync(object employeeData)
    {
        return await _odooService.CreateAsync("hr.employee", employeeData);
    }

    public async Task<bool> UpdateEmployeeAsync(int employeeId, Dictionary<string, object> employeeData)
    {
        return await _odooService.UpdateAsync("hr.employee", employeeId, employeeData);
    }

    public async Task<int?> GetAddressHomeIdByEmployeeIdAsync(int employeeId)
    {
        var filterData = new
        {
            domain = new List<object>
            {
                new object[] { "id", "=", employeeId }
            }
        };

        var fields = new[] { "address_id" };

        var results = await _odooService.SearchAsync("hr.employee", filterData, fields);

        if (results.Count > 0 &&
            results[0] is JsonElement record &&
            record.TryGetProperty("address_id", out var addressField) &&
            addressField.ValueKind == JsonValueKind.Array &&
            addressField.GetArrayLength() > 0)
        {
            return addressField[0].GetInt32();
        }

        return null;
    }


    public async Task<bool> UpdatePartnerAsync(int partnerId, Dictionary<string, object> updateData)
    {
        return await _odooService.UpdateAsync("res.partner", partnerId, updateData);
    }
}
