syntax = "proto3";

package flashdns.v1;

option java_package = "com.flashdns.v1";
option go_package = "flashdns/v1;flashdnsv1";

import "google/protobuf/timestamp.proto";
import "google/protobuf/empty.proto";

// =============================================================================
// Zone Service - Zone management
// =============================================================================

service ZoneService {
    // Zone CRUD
    rpc CreateZone(CreateZoneRequest) returns (CreateZoneResponse);
    rpc GetZone(GetZoneRequest) returns (GetZoneResponse);
    rpc ListZones(ListZonesRequest) returns (ListZonesResponse);
    rpc UpdateZone(UpdateZoneRequest) returns (UpdateZoneResponse);
    rpc DeleteZone(DeleteZoneRequest) returns (google.protobuf.Empty);

    // Zone status
    rpc EnableZone(EnableZoneRequest) returns (google.protobuf.Empty);
    rpc DisableZone(DisableZoneRequest) returns (google.protobuf.Empty);
}

// =============================================================================
// Record Service - DNS record management
// =============================================================================

service RecordService {
    // Record CRUD
    rpc CreateRecord(CreateRecordRequest) returns (CreateRecordResponse);
    rpc GetRecord(GetRecordRequest) returns (GetRecordResponse);
    rpc ListRecords(ListRecordsRequest) returns (ListRecordsResponse);
    rpc UpdateRecord(UpdateRecordRequest) returns (UpdateRecordResponse);
    rpc DeleteRecord(DeleteRecordRequest) returns (google.protobuf.Empty);

    // Batch operations
    rpc BatchCreateRecords(BatchCreateRecordsRequest) returns (BatchCreateRecordsResponse);
    rpc BatchDeleteRecords(BatchDeleteRecordsRequest) returns (google.protobuf.Empty);
}

// =============================================================================
// Common Types
// =============================================================================

message ZoneInfo {
    string id = 1;
    string name = 2;
    string org_id = 3;
    string project_id = 4;
    string status = 5;
    uint32 serial = 6;
    uint32 refresh = 7;
    uint32 retry = 8;
    uint32 expire = 9;
    uint32 minimum = 10;
    string primary_ns = 11;
    string admin_email = 12;
    google.protobuf.Timestamp created_at = 13;
    google.protobuf.Timestamp updated_at = 14;
    uint64 record_count = 15;
}

message RecordInfo {
    string id = 1;
    string zone_id = 2;
    string name = 3;
    string record_type = 4;
    uint32 ttl = 5;
    RecordData data = 6;
    bool enabled = 7;
    google.protobuf.Timestamp created_at = 8;
    google.protobuf.Timestamp updated_at = 9;
}

message RecordData {
    oneof data {
        ARecord a = 1;
        AaaaRecord aaaa = 2;
        CnameRecord cname = 3;
        MxRecord mx = 4;
        TxtRecord txt = 5;
        SrvRecord srv = 6;
        NsRecord ns = 7;
        PtrRecord ptr = 8;
        CaaRecord caa = 9;
    }
}

message ARecord {
    string address = 1;  // IPv4 address as string
}

message AaaaRecord {
    string address = 1;  // IPv6 address as string
}

message CnameRecord {
    string target = 1;
}

message MxRecord {
    uint32 preference = 1;
    string exchange = 2;
}

message TxtRecord {
    string text = 1;
}

message SrvRecord {
    uint32 priority = 1;
    uint32 weight = 2;
    uint32 port = 3;
    string target = 4;
}

message NsRecord {
    string nameserver = 1;
}

message PtrRecord {
    string target = 1;
}

message CaaRecord {
    uint32 flags = 1;
    string tag = 2;
    string value = 3;
}

// =============================================================================
// Zone Operations - Requests & Responses
// =============================================================================

message CreateZoneRequest {
    string name = 1;
    string org_id = 2;
    string project_id = 3;
    // Optional SOA parameters
    string primary_ns = 4;
    string admin_email = 5;
}

message CreateZoneResponse {
    ZoneInfo zone = 1;
}

message GetZoneRequest {
    oneof identifier {
        string id = 1;
        string name = 2;
    }
}

message GetZoneResponse {
    ZoneInfo zone = 1;
}

message ListZonesRequest {
    string org_id = 1;
    string project_id = 2;
    string name_filter = 3;
    uint32 page_size = 4;
    string page_token = 5;
}

message ListZonesResponse {
    repeated ZoneInfo zones = 1;
    string next_page_token = 2;
}

message UpdateZoneRequest {
    string id = 1;
    // Updatable fields
    optional uint32 refresh = 2;
    optional uint32 retry = 3;
    optional uint32 expire = 4;
    optional uint32 minimum = 5;
    optional string primary_ns = 6;
    optional string admin_email = 7;
}

message UpdateZoneResponse {
    ZoneInfo zone = 1;
}

message DeleteZoneRequest {
    string id = 1;
    bool force = 2;  // Delete even if records exist
}

message EnableZoneRequest {
    string id = 1;
}

message DisableZoneRequest {
    string id = 1;
}

// =============================================================================
// Record Operations - Requests & Responses
// =============================================================================

message CreateRecordRequest {
    string zone_id = 1;
    string name = 2;
    string record_type = 3;
    uint32 ttl = 4;
    RecordData data = 5;
}

message CreateRecordResponse {
    RecordInfo record = 1;
}

message GetRecordRequest {
    string id = 1;
}

message GetRecordResponse {
    RecordInfo record = 1;
}

message ListRecordsRequest {
    string zone_id = 1;
    string name_filter = 2;
    string type_filter = 3;
    uint32 page_size = 4;
    string page_token = 5;
}

message ListRecordsResponse {
    repeated RecordInfo records = 1;
    string next_page_token = 2;
}

message UpdateRecordRequest {
    string id = 1;
    optional uint32 ttl = 2;
    optional RecordData data = 3;
    optional bool enabled = 4;
}

message UpdateRecordResponse {
    RecordInfo record = 1;
}

message DeleteRecordRequest {
    string id = 1;
}

message BatchCreateRecordsRequest {
    string zone_id = 1;
    repeated CreateRecordRequest records = 2;
}

message BatchCreateRecordsResponse {
    repeated RecordInfo records = 1;
}

message BatchDeleteRecordsRequest {
    repeated string ids = 1;
}

// =============================================================================
// Reverse DNS Zone Service - Pattern-based PTR generation
// =============================================================================

service ReverseZoneService {
  rpc CreateReverseZone(CreateReverseZoneRequest) returns (ReverseZone);
  rpc GetReverseZone(GetReverseZoneRequest) returns (ReverseZone);
  rpc DeleteReverseZone(DeleteReverseZoneRequest) returns (DeleteReverseZoneResponse);
  rpc ListReverseZones(ListReverseZonesRequest) returns (ListReverseZonesResponse);
  rpc ResolvePtrForIp(ResolvePtrForIpRequest) returns (ResolvePtrForIpResponse);
}

message ReverseZone {
  string id = 1;
  string org_id = 2;
  optional string project_id = 3;
  string cidr = 4;
  string arpa_zone = 5;
  string ptr_pattern = 6;
  uint32 ttl = 7;
  uint64 created_at = 8;
  uint64 updated_at = 9;
}

message CreateReverseZoneRequest {
  string org_id = 1;
  optional string project_id = 2;
  string cidr = 3;
  string ptr_pattern = 4;
  uint32 ttl = 5;  // default: 3600
}

message GetReverseZoneRequest {
  string zone_id = 1;
}

message DeleteReverseZoneRequest {
  string zone_id = 1;
}

message DeleteReverseZoneResponse {
  bool success = 1;
}

message ListReverseZonesRequest {
  string org_id = 1;
  optional string project_id = 2;
}

message ListReverseZonesResponse {
  repeated ReverseZone zones = 1;
}

message ResolvePtrForIpRequest {
  string ip_address = 1;
}

message ResolvePtrForIpResponse {
  optional string ptr_record = 1;
  optional string reverse_zone_id = 2;
  bool found = 3;
}
