#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/epoll.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include "server.h"

static int kvs_write_u8(uint8_t **pp, uint8_t v) {
	uint8_t *p = *pp;
	*p = v;
	*pp = p + 1;
	return 0;
}

static int kvs_write_u32(uint8_t **pp, uint32_t v) {
	uint8_t *p = *pp;
	uint32_t be = htonl(v);
	memcpy(p, &be, 4);
	*pp = p + 4;
	return 0;
}

static uint64_t kvs_get_log_tail_offset(void) {
    int fd = open("kvs_cmd_log.db", O_RDONLY);
    if (fd < 0) {
        // 文件不存在：从 0 开始同步
        if (errno == ENOENT) return 0;
        // 其他错误：保守起见从 0 开始
        return 0;
    }

    off_t end = lseek(fd, 0, SEEK_END);
    close(fd);

    if (end < 0) return 0;
    return (uint64_t)end;   // 指向 EOF（下一次写入的位置）
}

int try_connect_master(char *ip, int port){
    
    struct sockaddr_in addr;
    memset(&addr, 0, sizeof(struct sockaddr));
    addr.sin_family = AF_INET;
    addr.sin_port = htons(port);
    addr.sin_addr.s_addr = inet_addr(ip);

    int rt = 1;
    while(1){
        int fd = socket(AF_INET, SOCK_STREAM, 0);
        if(fd < 0){
            continue;
        }
        
        if(0 == connect(fd, (struct sockaddr*)&addr, sizeof(struct sockaddr_in))){
            // [OP KVS_CMD_PSYNC=15][ARGC 1][ARGLEN 4][ARG offset]

            char buf[100];
            char *p = buf;

            kvs_write_u8((uint8_t**)&p, 15);
            kvs_write_u8((uint8_t**)&p, 1);

            uint64_t len = sizeof(uint64_t);
            kvs_write_u32((uint8_t**)&p, len);

            uint64_t offset = kvs_get_log_tail_offset(); 
            memcpy(p, (void*)&offset, len);
            p += sizeof(offset);

            send(fd, buf, p-buf, 0);
            recv(fd, buf, 100, 0);
            return fd;
        }
        
        close(fd);
    }

	return -1;
}